-1

任何人都知道为什么我可能会收到此错误?

我要做的就是保存时间选择器选择的时间......我确信这是我忽略的简单事情。

任何帮助是极大的赞赏!!!

阿曼尼·斯旺

import android.app.Activity; 
import android.app.AlertDialog;
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.TimePicker;

public class AddEditCountry extends Activity {

 private long rowID; 
 private EditText nameEt;
 private EditText capEt;
 private EditText codeEt;
 private TimePicker timeEt;

   @Override
   public void onCreate(Bundle savedInstanceState) 
   {
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.add_country);

      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"));  
         timeEt.setCurrentHour(extras.getString("time"));  
      }

      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(AddEditCountry.this);
                alert.setTitle(R.string.errorTitle); 
                alert.setMessage(R.string.errorMessage);
                alert.setPositiveButton(R.string.errorButton, null); 
                alert.show();
             }
          } 
     });
   }

   private void saveContact() 
   {
      DatabaseConnector dbConnector = new DatabaseConnector(this);

      if (getIntent().getExtras() == null)
      {
          dbConnector.insertContact(nameEt.getText().toString(),
                  capEt.getText().toString(),
                  timeEt.getCurrentHour().toString(),
                  codeEt.getText().toString());
      }
      else
      {
         dbConnector.updateContact(rowID,
            nameEt.getText().toString(),
            capEt.getText().toString(),
            timeEt.getCurrentHour().toString(),
            codeEt.getText().toString());
      }
   }
}
4

2 回答 2

1

问题是它setCurrentHour()需要一个整数,但是通过调用getString()你正在传递一个字符串,并且,正如错误所述,没有用于setCurrentHour(String).

你没有告诉我们 Intent 的 extras 设置在哪里,所以这个解决方案是一个猜测。但是尝试:

timeEt.setCurrentHour(extras.getInt("time"));
于 2013-03-21T22:53:07.147 回答
0

如果 extras 对象没有getInt()方法,请尝试以下操作:timeEt.setCurrentHour(Integer.valueof(extras.getString("time")));

于 2013-03-21T23:04:05.380 回答