Note that there are some explanatory texts on larger screens.

plurals
  1. PONull Pointer Exception with setOnClickListener()
    primarykey
    data
    text
    <p>I sometimes get a null pointer exception with my code. Oddly enough, this does not happen every time, and I'm not sure why. Help?</p> <pre><code>package net.obviam.droidz; public class HomeView extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.menu); Button page1 = (Button) findViewById(R.id.Button01); page1.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { // Intent i = getIntent(); Intent myIntent = new Intent(view.getContext(), DroidzActivity.class); startActivityForResult(myIntent, 0); } }); } protected void onDestroy() { // Log.d(TAG, "Destroying.."); super.onDestroy(); } @Override protected void onStop() { // Log.d(TAG, "Stopping..."); super.onStop(); } } </code></pre> <p>The logcat says that the problem is with line 19, the setOnClickListener. I'm not sure what's wrong with it though. It doesn't appear to be referencing anything.</p> <p>The Logcat reads like this:</p> <pre><code>02-17 16:52:13.816: E/AndroidRuntime(23756): FATAL EXCEPTION: main 02-17 16:52:13.816: E/AndroidRuntime(23756): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.obviam.droidz/net.obviam.droidz.HomeView}: java.lang.NullPointerException 02-17 16:52:13.816: E/AndroidRuntime(23756): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 02-17 16:52:13.816: E/AndroidRuntime(23756): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 02-17 16:52:13.816: E/AndroidRuntime(23756): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 02-17 16:52:13.816: E/AndroidRuntime(23756): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 02-17 16:52:13.816: E/AndroidRuntime(23756): at android.os.Handler.dispatchMessage(Handler.java:99) 02-17 16:52:13.816: E/AndroidRuntime(23756): at android.os.Looper.loop(Looper.java:130) 02-17 16:52:13.816: E/AndroidRuntime(23756): at android.app.ActivityThread.main(ActivityThread.java:3687) 02-17 16:52:13.816: E/AndroidRuntime(23756): at java.lang.reflect.Method.invokeNative(Native Method) 02-17 16:52:13.816: E/AndroidRuntime(23756): at java.lang.reflect.Method.invoke(Method.java:507) 02-17 16:52:13.816: E/AndroidRuntime(23756): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) 02-17 16:52:13.816: E/AndroidRuntime(23756): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 02-17 16:52:13.816: E/AndroidRuntime(23756): at dalvik.system.NativeStart.main(Native Method) 02-17 16:52:13.816: E/AndroidRuntime(23756): Caused by: java.lang.NullPointerException 02-17 16:52:13.816: E/AndroidRuntime(23756): at net.obviam.droidz.HomeView.onCreate(HomeView.java:19) 02-17 16:52:13.816: E/AndroidRuntime(23756): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 02-17 16:52:13.816: E/AndroidRuntime(23756): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 02-17 16:52:13.816: E/AndroidRuntime(23756): ... 11 more </code></pre> <p>And here is the manifest file:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="net.obviam.droidz" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="10" /&gt; &lt;application android:icon="@drawable/images" android:label="@string/app_name" android:debuggable="true"&gt; &lt;activity android:name=".HomeView" android:label="@string/app_name"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;activity android:name=".DroidzActivity"&gt;&lt;/activity&gt; &lt;/application&gt; </code></pre> <p></p> <p>And here is the xml file that the button is found in:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"&gt; &lt;Button android:text="Start Game!" android:id="@+id/Button01" android:layout_width="250dp" android:textSize="18dp" android:layout_height="55dp"&gt; &lt;/Button&gt; &lt;/LinearLayout&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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