0

问题:在第 65 行强制关闭 - saveContact();

笔记:

大家好 - 我正在尝试在我的数据库中保存一些数据,但应用程序不断崩溃。我对这一切有点陌生,所以我想知道是否有人可以解释我做错了什么。我确定这很简单......但我不知道如何解决它。

非常感谢任何和所有帮助!

菜鸟忍者

    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;
         private TimePicker minEt;
         public static final String KEY_BUNDLE_TIME = "time";
         public static final String KEY_BUNDLE_MIN = "min";

           @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);
              minEt = (TimePicker) findViewById(R.id.minEdit);


              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.containsKey(KEY_BUNDLE_TIME) ? extras.getInt(KEY_BUNDLE_TIME) : 0);
                 minEt.setCurrentMinute(extras.getInt("min"));
              }

              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(),
                              minEt.getCurrentMinute().toString(),
                              codeEt.getText().toString(),
                              timePicker.getCurrentHour().toString(),
                              timePicker.getCurrentMinute().toString());

                  }
                  else
                  {
                     dbConnector.updateContact(rowID,
                        nameEt.getText().toString(),
                        capEt.getText().toString(),
                        timeEt.getCurrentHour().toString(),
                        minEt.getCurrentMinute().toString(),/* Storing as String*/
                        codeEt.getText().toString());
                  }
               }
    }














    import android.content.ContentValues;
    import android.content.Context;
    import android.database.Cursor;
    import android.database.SQLException;
    import android.database.sqlite.SQLiteDatabase;



    public class DatabaseConnector {

        public static final String KEY_BUNDLE_TIME = "time";
        public static final String KEY_BUNDLE_MIN = "min";
        private static final String DB_NAME = "WorldCountries";
        private SQLiteDatabase database;
        private DatabaseOpenHelper dbOpenHelper;

        public DatabaseConnector(Context context) {
            dbOpenHelper = new DatabaseOpenHelper(context, DB_NAME, null, 1);
        }

           public void open() throws SQLException 
           {
              //open database in reading/writing mode
              database = dbOpenHelper.getWritableDatabase();
           } 

           public void close() 
           {
              if (database != null)
                 database.close();
           }       

           public void insertContact(String name, String cap, String code, String time, String min) 
                   {
                      ContentValues newCon = new ContentValues();
                      newCon.put("name", name);
                      newCon.put("cap", cap);
                      newCon.put("time", time);
                      newCon.put("min", min);
                      newCon.put("code", code);
                      newCon.put(AddEditCountry.KEY_BUNDLE_TIME, time);
                      newCon.put(AddEditCountry.KEY_BUNDLE_MIN, min);

                      open();
                      database.insert("country", null, newCon);
                      close();
                   }


                   public void updateContact(long id, String name, String cap,String code, String time, String min) 
                   {
                      ContentValues editCon = new ContentValues();
                      editCon.put("name", name);
                      editCon.put("cap", cap);
                      editCon.put("time", time);
                      editCon.put("min", min);
                      editCon.put("code", code);
                      editCon.put(AddEditCountry.KEY_BUNDLE_TIME, time);
                      editCon.put(AddEditCountry.KEY_BUNDLE_MIN, min);
                      open();
                      database.update("country", editCon, "_id=" + id, null);
                      close();
                   }


                   public Cursor getAllContacts() 
                   {
                      return database.query("country", new String[] {"_id", "name"}, 
                         null, null, null, null, "name");
                   }

                   public Cursor getOneContact(long id) 
                   {
                      return database.query("country", null, "_id=" + id, null, null, null, null);
                   }

                   public void deleteContact(long id) 
                   {
                      open(); 
                      database.delete("country", "_id=" + id, null);
                      close();
                   }
    }


PROBLEMS:


timePicker cannot be resolved   AddEditCountry.java line 104
minEdit cannot be resolved or is not a field    AddEditCountry.java line 37 
timePicker cannot be resolved   AddEditCountry.java line 103    
4

2 回答 2

1

我认为问题是这一行:

minEt.getCurrentMinute().toString()

您正在尝试从中获取TimePicker分配给的数据NULL

private TimePicker minEt;

这就是您的应用程序崩溃的原因。解决方案是初始化您的 TimePicker,它应该可以工作。因此,从您初始化它的行中删除注释。

于 2013-03-24T22:40:53.940 回答
0

您是否在任何字段中设置了数据?您提供的堆栈跟踪说这是由于第 101 行的 NPE

03-24 00:49:10.643: E/AndroidRuntime(6809):     at com.nfc.linkingmanager.AddEditCountry.saveContact(AddEditCountry.java:101)

在不知道哪些线或区域的情况下,我只是在猜测,但您可以更改您的 saveContact 以使用:

findViewById(R.id.nameEdit).getText().toString()

如:

dbConnector.insertContact(
   findViewById(R.id.nameEdit).getText().toString(),
   findViewById(R.id.nameEdit).getText().toString().getText().toString(),
   etc...

基本上,您的一个 var 为 null 并且您正在尝试使用它。

于 2013-03-24T22:41:00.863 回答