Note that there are some explanatory texts on larger screens.

plurals
  1. PONewbie Android NullPointerException Trouble Shooting
    text
    copied!<p>I am having a similar problem as the person here: <a href="https://stackoverflow.com/questions/7977126/android-activity-error">Android activity error</a> I've read through the post. I also read through other posts. But I can't seem to get my Android application running. </p> <p>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. </p> <p>Main.java</p> <pre><code>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)); } }); } } </code></pre> <p>Second.java</p> <pre><code>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); } } </code></pre> <p>Error Message</p> <pre><code>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 </code></pre> <p>I really appreciate your help. I really want to learn Android, and these trouble shooting problems have always caused a lot of pain. </p> <p>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. </p> <p>main.xml</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /&gt; &lt;/LinearLayout&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload