I am having a similar problem as the person here: Android activity error I’ve read through the post. I also read through other posts. But I can’t seem to get my Android application running.
Description: I have one button. I want this button to work. I keep getting an NPE because of an OnClickListener. This is virtually an identical problem as the above post, but mine is behaving a little differently. Please help me. Thank you very much.
Main.java
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(Main.this, Second.class));
}
});
}
}
Second.java
import android.app.Activity;
import android.os.Bundle;
public class Second extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
}
Error Message
12-27 09:19:10.729: W/dalvikvm(923): threadid=1: thread exiting with uncaught exception (group=0x409951f8)
12-27 09:19:10.749: E/AndroidRuntime(923): FATAL EXCEPTION: main
12-27 09:19:10.749: E/AndroidRuntime(923): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.harryoh.firstapp/com.harryoh.firstapp.Main}: java.lang.NullPointerException
12-27 09:19:10.749: E/AndroidRuntime(923): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-27 09:19:10.749: E/AndroidRuntime(923): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-27 09:19:10.749: E/AndroidRuntime(923): at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-27 09:19:10.749: E/AndroidRuntime(923): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-27 09:19:10.749: E/AndroidRuntime(923): at android.os.Handler.dispatchMessage(Handler.java:99)
12-27 09:19:10.749: E/AndroidRuntime(923): at android.os.Looper.loop(Looper.java:137)
12-27 09:19:10.749: E/AndroidRuntime(923): at android.app.ActivityThread.main(ActivityThread.java:4340)
12-27 09:19:10.749: E/AndroidRuntime(923): at java.lang.reflect.Method.invokeNative(Native Method)
12-27 09:19:10.749: E/AndroidRuntime(923): at java.lang.reflect.Method.invoke(Method.java:511)
12-27 09:19:10.749: E/AndroidRuntime(923): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-27 09:19:10.749: E/AndroidRuntime(923): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-27 09:19:10.749: E/AndroidRuntime(923): at dalvik.system.NativeStart.main(Native Method)
12-27 09:19:10.749: E/AndroidRuntime(923): Caused by: java.lang.NullPointerException
12-27 09:19:10.749: E/AndroidRuntime(923): at com.harryoh.firstapp.Main.onCreate(Main.java:19)
12-27 09:19:10.749: E/AndroidRuntime(923): at android.app.Activity.performCreate(Activity.java:4465)
12-27 09:19:10.749: E/AndroidRuntime(923): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-27 09:19:10.749: E/AndroidRuntime(923): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
12-27 09:19:10.749: E/AndroidRuntime(923): ... 11 more
I really appreciate your help. I really want to learn Android, and these trouble shooting problems have always caused a lot of pain.
EDIT: Thanks for your help. I will return the favor to this community once I am able… I haven’t gone through your inputs yet, since I wanted to provide this as requested. I will work on it now. I hope it works.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
This has happened to me numerous times while messing with layouts. If you just recently added the button try refreshing the project and clean rebuilding and then see if it picks up the button. For some reason if you add widgets and try to test them immediately after it doesn’t actually update.
EDIT: After seeing your main.xml it looks like you dont even have a button set up. This would cause the null pointer exception because its looking for something that doesn’t exist.