Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Intent Button NullPointerException
    primarykey
    data
    text
    <p>I'm trying have a button start another class. I get a null pointer exception which I found using logcat and I don't know why.</p> <pre class="lang-java prettyprint-override"><code> package com.Juggle2; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; public class Juggle2 extends Activity { public static final String LOG_TAG = "Juggle2"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); Button StartGameButton = (Button) findViewById(R.id.StartGame); StartGameButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent i = new Intent(Juggle2.this, StartGame.class); startActivity(i); } }); </code></pre> <p>And my XML manifest:</p> <pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.Juggle2" android:versionCode="1" android:versionName="1.0"&gt; &lt;uses-sdk android:minSdkVersion="10" /&gt; &lt;application android:icon="@drawable/icon" android:label="@string/app_name"&gt; &lt;activity android:name=".Juggle2" 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=".Panel"&gt;&lt;/activity&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p>Here is the log </p> <pre><code> 05-27 21:12:07.749: ERROR/AndroidRuntime(6705): FATAL EXCEPTION: main 05-27 21:12:07.749: ERROR/AndroidRuntime(6705): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Juggle2/com.Juggle2.Juggle2}: java.lang.NullPointerException 05-27 21:12:07.749: ERROR/AndroidRuntime(6705): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736) 05-27 21:12:07.749: ERROR/AndroidRuntime(6705): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752) 05-27 21:12:07.749: ERROR/AndroidRuntime(6705): at android.app.ActivityThread.access$1500(ActivityThread.java:123) 05-27 21:12:07.749: ERROR/AndroidRuntime(6705): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993) 05-27 21:12:07.749: ERROR/AndroidRuntime(6705): at android.os.Handler.dispatchMessage(Handler.java:99) 05-27 21:12:07.749: ERROR/AndroidRuntime(6705): at android.os.Looper.loop(Looper.java:126) 05-27 21:12:07.749: ERROR/AndroidRuntime(6705): at android.app.ActivityThread.main(ActivityThread.java:3997) 05-27 21:12:07.749: ERROR/AndroidRuntime(6705): at java.lang.reflect.Method.invokeNative(Native Method) 05-27 21:12:07.749: ERROR/AndroidRuntime(6705): at java.lang.reflect.Method.invoke(Method.java:491) 05-27 21:12:07.749: ERROR/AndroidRuntime(6705): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 05-27 21:12:07.749: ERROR/AndroidRuntime(6705): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 05-27 21:12:07.749: ERROR/AndroidRuntime(6705): at dalvik.system.NativeStart.main(Native Method) 05-27 21:12:07.749: ERROR/AndroidRuntime(6705): Caused by: java.lang.NullPointerException 05-27 21:12:07.749: ERROR/AndroidRuntime(6705): at com.Juggle2.Juggle2.onCreate(Juggle2.java:24) 05-27 21:12:07.749: ERROR/AndroidRuntime(6705): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048) 05-27 21:12:07.749: ERROR/AndroidRuntime(6705): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700) 05-27 21:12:07.749: ERROR/AndroidRuntime(6705): ... 11 more </code></pre> <p>And inside the main.xml:</p> <p>There is this button</p> <pre><code>&lt;Button android:id="@+id/StartGame" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/startgame_button" &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