Search code examples
androidforceclose

Extremely simple android app keeps FC'ing


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

`<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width ="fill_parent"
android:layout_height = "fill_parent">
<TableLayout 
android:id="@+id/details"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" 
android:layout_alignParentBottom="true">

<TableRow>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/firstName" />

<EditText
    android:id="@+id/editText1"
    android:ems="10" 
/>
</TableRow>
<TableRow>
<TextView
android:id="@+id/textView2"
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:text="@string/lastName" />
<EditText
    android:id="@+id/editText2"
     />
</TableRow>
<TableRow>
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Save" />
</TableRow>
</TableLayout>

<ListView
 android:id = "@+id/listView1"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentTop="true"
 android:layout_above="@id/details"
 />

</RelativeLayout>

`

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!


Solution

  • 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;