Nidhin_toms
Nidhin_toms

Reputation: 737

Activity not launching

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

Hello , I am trying to launch an activity and when android tries to launch the above activity ( workmemo ) , it fails. Here is my error log below. any ideas?

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

Upvotes: 0

Views: 127

Answers (4)

Marcel Bro
Marcel Bro

Reputation: 5025

Your question has already been answered, I'd just tell you how to find out what's going on when looking at error log.

Go from the end and look for "Caused by" phrase. There you'll find an Exception what actually caused your crash. On lines below search for your package and on the end of that line in brackets you'll see your class (and number of the line in your code) where the Exception was thrown.

It works with these simple, common mistakes. Hope it helps you solve these problems in the future.

Upvotes: 1

Samir Mangroliya
Samir Mangroliya

Reputation: 40416

editTextWorkmemo1 is null.I mean uninitiallized.give reference from xml layout like

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

Upvotes: 1

xaviert
xaviert

Reputation: 5932

Your variable editTextWorkmemo1 is unitialized but you're calling getText() on it at line 32. This causes the NullPointerException.

Upvotes: 4

Alexandre B.
Alexandre B.

Reputation: 495

I think you forget to specify editTextWorkmemo1 before q1=editTextWorkmemo1.getText().toString();

Upvotes: 2

Related Questions