gpt4 book ai didi

java - Android Intent 按钮 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 04:40:00 27 4
gpt4 key购买 nike

我正在尝试让一个按钮开始另一个类。我使用 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);
}
});

还有我的 XML list :

<?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>

这是日志

    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

在 main.xml 中:

有这个按钮

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

最佳答案

您的面板类不是 Activity 而是 SurfaceView。因此,创建一个新的 Activity 类(可能称为 PanelActivity)并在其 onCreate() 调用 setContentView(new Panel());

此外,这一行:
Intent i = new Intent().setClass(Juggle2.this, Panel.class );
应该看起来像:
Intent i = new Intent(Juggle2.this, PanelActivity.class);

关于java - Android Intent 按钮 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6157557/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com