Extremely simple android app keeps FC'ing-Collection of common programming errors

Hey I just started dabbling with android programming, and from my short period of time with it I’m a bit frustrated. I can get no errors in eclipse, but when I start my app it immediately force closes. I’m following an android tutorial book verbatim, and still I’m getting errors. My aim here is to create a list view inside a relative layout. Also, for some reason when I assign p.firstName or p.lastName it’ll force close as well, but anyways here’s my code. I’m using API level 15 (ie. Android 4.0.3) if that makes a difference.

 package matthews.zack.test;
 import android.app.Activity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
   public class _testActivity extends Activity {

Button save;
People p = new People();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //onInitialize();
}

private void onInitialize()
{
     save = (Button)findViewById(android.R.id.button1);
     save.setOnClickListener(new View.OnClickListener() {


        public void onClick(View v) {
            // TODO Auto-generated method stub
            EditText firstName = (EditText)findViewById(android.R.id.text1);
            EditText lastName = (EditText)findViewById(android.R.id.text2);
            p.setFirstName("Hai");//firstName.getText().toString());
            p.setLastName("Hi");//lastName.getText().toString());
            //((EditText)findViewById(android.R.id.text1)).setText("This the tune bada bing bada boom");
        }
    });
}

}

`
and here’s my XML code

`






















`

Here’s my error output

06-12 00:32:44.871: D/AndroidRuntime(30530): Shutting down VM
06-12 00:32:44.871: W/dalvikvm(30530): threadid=1: thread exiting with uncaught exception (group=0x40a3b1f8)
06-12 00:32:44.879: E/AndroidRuntime(30530): FATAL EXCEPTION: main
06-12 00:32:44.879: E/AndroidRuntime(30530): java.lang.RuntimeException: Unable to start activity ComponentInfo{matthews.zack.test/matthews.zack.test._testActivity}: java.lang.NullPointerException
06-12 00:32:44.879: E/AndroidRuntime(30530):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-12 00:32:44.879: E/AndroidRuntime(30530):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-12 00:32:44.879: E/AndroidRuntime(30530):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-12 00:32:44.879: E/AndroidRuntime(30530):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-12 00:32:44.879: E/AndroidRuntime(30530):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-12 00:32:44.879: E/AndroidRuntime(30530):    at android.os.Looper.loop(Looper.java:137)
06-12 00:32:44.879: E/AndroidRuntime(30530):    at android.app.ActivityThread.main(ActivityThread.java:4424)
06-12 00:32:44.879: E/AndroidRuntime(30530):    at java.lang.reflect.Method.invokeNative(Native Method)
06-12 00:32:44.879: E/AndroidRuntime(30530):    at java.lang.reflect.Method.invoke(Method.java:511)
06-12 00:32:44.879: E/AndroidRuntime(30530):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
06-12 00:32:44.879: E/AndroidRuntime(30530):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
06-12 00:32:44.879: E/AndroidRuntime(30530):    at dalvik.system.NativeStart.main(Native Method)
06-12 00:32:44.879: E/AndroidRuntime(30530): Caused by: java.lang.NullPointerException
06-12 00:32:44.879: E/AndroidRuntime(30530):    at matthews.zack.test._testActivity.onInitialize(_testActivity.java:24)
06-12 00:32:44.879: E/AndroidRuntime(30530):    at matthews.zack.test._testActivity.onCreate(_testActivity.java:18)
06-12 00:32:44.879: E/AndroidRuntime(30530):    at android.app.Activity.performCreate(Activity.java:4465)
06-12 00:32:44.879: E/AndroidRuntime(30530):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-12 00:32:44.879: E/AndroidRuntime(30530):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-12 00:32:44.879: E/AndroidRuntime(30530):    ... 11 more

It’s a bit frustrating that android will force close without telling me the exception. I would appreciate the help. Thanks!

  1. Here is the mistake:

    save = (Button)findViewById(android.R.id.button1);
    

    You better do something like that:

    save = (Button)findViewById(R.id.button1);
    

    And import the right R file:

    import com.yournamespace.R;
    
  2. You’re searching the Button using a wrong id. It should be:

    save = (Button)findViewById(R.id.button1);
    

    Also, you’re doing the same thing for the EditText. Modify it like this:

    EditText firstName = (EditText)findViewById(R.id.text1);
    EditText lastName = (EditText)findViewById(R.id.text2);
    

    The only time when you would use android.R.layout.some_id_here is when you’re using a framework defined id(for example you would use android.R.id.list for a ListView element used in a layout in a ListActivity).

  3. It’s a bit frustrating that android will force close without telling me the exception.

    It’s right there in the log, its a java.lang.RuntimeException and further says

    Caused by: java.lang.NullPointerException

    You are using findViewById with an id not defined in your layout. The function cannot find any view with that id and returns null. You later try to dereference such null and hence the NullPointerException exception.

  4. android.R.id.button1 should be R.id.button1

Originally posted 2013-11-16 20:52:19.