简体   繁体   English

Android Intent Button NullPointerException

[英]Android Intent Button NullPointerException

I'm trying have a button start another class.我正在尝试用一个按钮启动另一个 class。 I get a null pointer exception which I found using logcat and I don't know why.我得到一个 null 指针异常,我使用 logcat 发现它,但我不知道为什么。

    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);
                }
            });

And my XML manifest:还有我的 XML 清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.Juggle2"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Juggle2"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
      <activity android:name=".Panel"></activity>
    </application>
</manifest>

Here is the log这是日志

    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

And inside the main.xml:而在main.xml里面:

There is this button有这个按钮

<Button
android:id="@+id/StartGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/startgame_button"
>

Your panel class is not an Activity but a SurfaceView.您的面板 class 不是 Activity 而是 SurfaceView。 So, create a new Activity class (maybe called PanelActivity) and in its onCreate() call setContentView(new Panel());因此,创建一个新的活动 class(可能称为 PanelActivity)并在其 onCreate() 调用setContentView(new Panel());

Also, this line:另外,这一行:
Intent i = new Intent().setClass(Juggle2.this, Panel.class );
Should look like:应该看起来像:
Intent i = new Intent(Juggle2.this, PanelActivity.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM