活动未启动

时间:2012-04-24 14:23:45

标签: android database android-activity

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 个答案:

答案 0 :(得分:4)

您的变量editTextWorkmemo1已经缩小,但您在第32行调用了getText()。这会导致NullPointerException

答案 1 :(得分:2)

我想你忘了在q1=editTextWorkmemo1.getText().toString();

之前指定editTextWorkmemo1

答案 2 :(得分:1)

editTextWorkmemo1为null。我的意思是来自xml布局的uninitiallized.give,如

  

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

答案 3 :(得分:1)

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

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

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