0
public class workmemo extends Activity{

   EditText editTextWorkmemo1;
   EditText editTextWorkmemo2;
   String q1;
   String q2;

   Button ButtonWorkMemo1 ;
   WorkmemoDatabaseHelper dbh2 = new WorkmemoDatabaseHelper(workmemo.this);

   public void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);
      setContentView(R.layout.workmemo);

      q1=editTextWorkmemo1.getText().toString();

      String currentDateTimeString = DateFormat.getDateTimeInstance().format(
                                                                   new Date());
      editTextWorkmemo2=(EditText)findViewById(R.id.editTextWorkmemo2);
      editTextWorkmemo2.setText(currentDateTimeString);
      q2=currentDateTimeString;

      ButtonWorkMemo1 = (Button) findViewById(R.id.ButtonWorkMemo1);
      ButtonWorkMemo1.setOnClickListener(new clicker());


   }

   class clicker implements Button.OnClickListener{
      public void onClick(View v) {                        
         if(v== ButtonWorkMemo1) {
            //addEvent(q1,q2);
            Intent viewDataIntent = new Intent(workmemo.this, Database.class);
            startActivity(viewDataIntent);
         }
      }
   }

    public void addEvent ( String q1,String q) {
       SQLiteDatabase db = dbh2.getWritableDatabase();
       ContentValues values = new ContentValues();

       values.put(WorkmemoDatabaseHelper.memo, q1);
       values.put(WorkmemoDatabaseHelper.date, q2);

       db.insert(WorkmemoDatabaseHelper.TABLE_NAME, null, values);      
    }
}

您好,我正在尝试启动一个活动,当 android 尝试启动上述活动(workmemo)时,它失败了。这是我的错误日志。有任何想法吗?

04-24 12:46:06.955: W/dalvikvm(548): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-24 12:46:06.984: E/AndroidRuntime(548): FATAL EXCEPTION: main
04-24 12:46:06.984: E/AndroidRuntime(548): java.lang.RuntimeException: Unable to start activity ComponentInfo{nidhin.organizer/nidhin.organizer.workmemo}: java.lang.NullPointerException
04-24 12:46:06.984: E/AndroidRuntime(548):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
04-24 12:46:06.984: E/AndroidRuntime(548):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-24 12:46:06.984: E/AndroidRuntime(548):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-24 12:46:06.984: E/AndroidRuntime(548):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-24 12:46:06.984: E/AndroidRuntime(548):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-24 12:46:06.984: E/AndroidRuntime(548):  at android.os.Looper.loop(Looper.java:137)
04-24 12:46:06.984: E/AndroidRuntime(548):  at android.app.ActivityThread.main(ActivityThread.java:4424)
04-24 12:46:06.984: E/AndroidRuntime(548):  at java.lang.reflect.Method.invokeNative(Native Method)
04-24 12:46:06.984: E/AndroidRuntime(548):  at java.lang.reflect.Method.invoke(Method.java:511)
04-24 12:46:06.984: E/AndroidRuntime(548):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-24 12:46:06.984: E/AndroidRuntime(548):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-24 12:46:06.984: E/AndroidRuntime(548):  at dalvik.system.NativeStart.main(Native Method)
04-24 12:46:06.984: E/AndroidRuntime(548): Caused by: java.lang.NullPointerException
04-24 12:46:06.984: E/AndroidRuntime(548):  at nidhin.organizer.workmemo.onCreate(workmemo.java:32)
04-24 12:46:06.984: E/AndroidRuntime(548):  at android.app.Activity.performCreate(Activity.java:4465)
04-24 12:46:06.984: E/AndroidRuntime(548):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-24 12:46:06.984: E/AndroidRuntime(548):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-24 12:46:06.984: E/AndroidRuntime(548):  ... 11 more
4

4 回答 4

4

您的变量editTextWorkmemo1未初始化,但您getText()在第 32 行调用它。这会导致NullPointerException.

于 2012-04-24T14:28:50.683 回答
2

我认为您之前忘记指定 editTextWorkmemo1q1=editTextWorkmemo1.getText().toString();

于 2012-04-24T14:27:21.960 回答
1

editTextWorkmemo1是空的。我的意思是 uninitialized.give 来自 xml 布局的引用,例如

EditText editTextWorkmemo1=(EditText)findViewByid(R.id.YOURIDINXML);

于 2012-04-24T14:30:35.097 回答
1

您的问题已经得到解答,我只是告诉您如何在查看错误日志时找出发生了什么。

从最后开始寻找“Caused by”短语。在那里你会发现一个异常,它实际上导致了你的崩溃。在下面的行中搜索您的包,并在该行末尾的括号中,您将看到抛出异常的类(以及代码中的行号)。

它适用于这些简单、常见的错误。希望它可以帮助您在将来解决这些问题。

于 2012-04-24T14:39:25.373 回答