Note that there are some explanatory texts on larger screens.

plurals
  1. POThe local variable may not have been initialized
    primarykey
    data
    text
    <p>ISSUE:</p> <p>I have a TimePicker which I'd like to use to save the value of the long/string "minutes" which represents the total number of minutes between the current time and the time selected by the timepicker.</p> <p>For example: If it is currently 7:30 and the user selects 8:30 it will save 60 minutes as the value of minutes.</p> <p>I've managed to implement the following source code (shown below) however I'm getting a few errors stating:</p> <pre><code>"The local variable tp may not have been initialized" </code></pre> <p>Using the method suggested in the comments below - although I'm following the instructions precisely. </p> <p>COMPILER PROBLEMS:</p> <pre><code>Description Resource Path Location Type The local variable tp may not have been initialized AddEditDevice.java line 119 Java Problem The local variable tp may not have been initialized AddEditDevice.java line 120 Java Problem </code></pre> <p>SOURCE:</p> <pre><code>import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.AsyncTask; import android.os.Bundle; import android.view.ViewGroup; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.TextView; import android.widget.TimePicker; import java.text.DecimalFormat; import android.util.Log; import java.util.Calendar; public class AddEditDevice extends Activity { private long rowID; private EditText nameEt; private EditText capEt; private EditText codeEt; private TimePicker timeEt; private TextView ssidTextView; Calendar cal = Calendar.getInstance(); TimePicker tp; // @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.add_country); WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo info = wifi.getConnectionInfo(); String ssidString = info.getSSID(); if (ssidString.startsWith("\"") &amp;&amp; ssidString.endsWith("\"")){ ssidString = ssidString.substring(1, ssidString.length()-1); } //TextView ssidTextView = (TextView) findViewById(R.id.wifiSSID); ssidTextView = (TextView) findViewById(R.id.wifiSSID); ssidTextView.setText(ssidString); nameEt = (EditText) findViewById(R.id.nameEdit); capEt = (EditText) findViewById(R.id.capEdit); codeEt = (EditText) findViewById(R.id.codeEdit); timeEt = (TimePicker) findViewById(R.id.timeEdit); Bundle extras = getIntent().getExtras(); if (extras != null) { rowID = extras.getLong("row_id"); nameEt.setText(extras.getString("name")); capEt.setText(extras.getString("cap")); codeEt.setText(extras.getString("code")); String time = extras.getString("time"); String[] parts = time.split(":"); timeEt.setCurrentHour(Integer.valueOf(parts[0])); timeEt.setCurrentMinute(Integer.valueOf(parts[1])); timeEt.setIs24HourView(false); } Button saveButton =(Button) findViewById(R.id.saveBtn); saveButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (nameEt.getText().length() != 0) { AsyncTask&lt;Object, Object, Object&gt; saveContactTask = new AsyncTask&lt;Object, Object, Object&gt;() { @Override protected Object doInBackground(Object... params) { saveContact(); return null; } @Override protected void onPostExecute(Object result) { finish(); } }; saveContactTask.execute((Object[]) null); } else { AlertDialog.Builder alert = new AlertDialog.Builder(AddEditDevice.this); alert.setTitle(R.string.errorTitle); alert.setMessage(R.string.errorMessage); alert.setPositiveButton(R.string.errorButton, null); alert.show(); } } });} public static void main(String[] args) { Calendar cal = Calendar.getInstance(); TimePicker tp; cal.set(Calendar.HOUR_OF_DAY, tp.getCurrentHour()); cal.set(Calendar.MINUTE, tp.getCurrentMinute()); long minutes = (cal.getTimeInMillis() - Calendar.getInstance().getTimeInMillis()) / 1000 / 60; } private void saveContact() { DatabaseConnector dbConnector = new DatabaseConnector(this); if (getIntent().getExtras() == null) { // Log.i("Test for Null", ""+dbConnector+" "+nameEt+" "+capEt+" "+timeEt+" "+codeEt+" "+ssidTextView); dbConnector.insertContact(nameEt.getText().toString(), capEt.getText().toString(), timeEt.getCurrentHour().toString() + ":" + timeEt.getCurrentMinute().toString(), codeEt.getText().toString(), ssidTextView.getText().toString()); } else { dbConnector.updateContact(rowID, nameEt.getText().toString(), capEt.getText().toString(), timeEt.getCurrentHour().toString() + ":" + timeEt.getCurrentMinute().toString(), codeEt.getText().toString(), ssidTextView.getText().toString()); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload