0

问题:

我有一个 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());
              }
           }
}
4

1 回答 1

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);
    }

}

420:54我当地时间返回。

于 2013-04-22T10:27:48.137 回答