局部变量可能尚未初始化

时间:2013-04-22 10:24:21

标签: java android timepicker

ISSUE:

我有一个TimePicker,我想用它来保存long / string“minutes”的值,它表示当前时间与timepicker选择的时间之间的总分钟数。

例如:如果当前是7:30,并且用户选择8:30,则会将60分钟保存为分钟值。

我设法实现了以下源代码(如下所示)但是我收到了一些错误说明:

"The local variable tp may not have been initialized"

使用下面评论中建议的方法 - 尽管我正在严格遵循说明。

编制者问题:

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

来源:

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("\"") && 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<Object, Object, Object> saveContactTask = 
                       new AsyncTask<Object, Object, Object>() 
                       {
                          @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());
              }
           }
}

1 个答案:

答案 0 :(得分:0)

使用java.util.Calendar获取当前值并减去。

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;

编辑:尝试使用此Java控制台应用程序。证明它有效。

import java.util.Calendar;

public class Main {

    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();

        cal.set(Calendar.HOUR_OF_DAY, 20);
        cal.set(Calendar.MINUTE, 58);

        long minutes = (cal.getTimeInMillis() - Calendar.getInstance().getTimeInMillis()) / 1000 / 60;

        System.out.println(minutes);
    }

}

在当地时间的4返回20:54