按钮的 OnClickListener 上出现 NullPointerException

发布于 2024-12-11 17:50:10 字数 9123 浏览 1 评论 0原文

我正在为人工智能学期项目编写 Android 游戏应用程序。我在主类的第 38 行收到 NullPointerException,它是启动屏幕中新游戏按钮的 setOnClickListener。

相关部分 WW3Activity 类:

public class WW3Activity extends Activity
{
public boolean DebugMode = false;
private String buildMenuEmplacement = ""; 

//[Initialization] Called when the app is first launched
@Override
public void onCreate(Bundle savedInstanceState)
{           
    super.onCreate(savedInstanceState);
    setContentView(R.layout.startscreen);

    //[Initialization] Makes the button resources available to the class
    final Button newGame = (Button) findViewById(R.id.buttonNewGame);
    final Button loadGame = (Button) findViewById(R.id.buttonLoadGame);
    final Button exitGame = (Button) findViewById(R.id.buttonExit);
    final Button about = (Button) findViewById(R.id.buttonAbout);
    final Button troll = (Button) findViewById(R.id.buttonTroll);
    final Button debug = (Button) findViewById(R.id.buttonDebug);

    //[Action] When user pushes the New Game button
    newGame.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            // Perform action on click
            setContentView(R.layout.main);
            buttonInitialize();
        }
    });
    //[Action] When user pushes the Exit Game button
    exitGame.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            // Perform action on click
            finish();
        }
    });
    //[Action] When user pushes the Debug Mode button
    exitGame.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            // Perform action on click
            DebugMode = true;
        }


      });
    }

    //[Initialization] Gets the emplacements ready to be pushed
   public void buttonInitialize()
   {
        final ImageView pe1 = (ImageView) findViewById(R.id.playerEmplacement1);
        final ImageView pe2 = (ImageView) findViewById(R.id.playerEmplacement2);
        final ImageView pe3 = (ImageView) findViewById(R.id.playerEmplacement3);
        final ImageView pc = (ImageView) findViewById(R.id.playerCity);
        final ImageView ee1 = (ImageView) findViewById(R.id.enemyEmplacement1);
        final ImageView ee2 = (ImageView) findViewById(R.id.enemyEmplacement2);
        final ImageView ee3 = (ImageView) findViewById(R.id.enemyEmplacement3);
        final ImageView ec = (ImageView) findViewById(R.id.enemyCity);

        registerForContextMenu(findViewById(R.id.playerEmplacement1));
        registerForContextMenu(findViewById(R.id.playerEmplacement2));
        registerForContextMenu(findViewById(R.id.playerEmplacement3));
        registerForContextMenu(findViewById(R.id.enemyEmplacement1));
        registerForContextMenu(findViewById(R.id.enemyEmplacement2));
        registerForContextMenu(findViewById(R.id.enemyEmplacement3));

        /*empAction(pe1);
        empAction(pe2);
        empAction(pe3);
        empAction(ee1);
        empAction(ee2);
        empAction(ee3);
        empAction(ec);
        empAction(pc);*/
    }

和 LogCat:

10-24 13:29:12.297: D/dalvikvm(440): GC_FOR_ALLOC freed 40K, 4% free 6356K/6595K, paused 177ms
10-24 13:29:12.303: I/dalvikvm-heap(440): Grow heap (frag case) to 6.653MB for 409616-byte allocation
10-24 13:29:12.503: D/dalvikvm(440): GC_FOR_ALLOC freed <1K, 5% free 6755K/7047K, paused 128ms
10-24 13:29:12.614: D/dalvikvm(440): GC_FOR_ALLOC freed 400K, 8% free 6762K/7303K, paused 57ms
10-24 13:29:12.663: D/AndroidRuntime(440): Shutting down VM
10-24 13:29:12.663: W/dalvikvm(440): threadid=1: thread exiting with uncaught exception (group=0x40014760)
10-24 13:29:12.683: E/AndroidRuntime(440): FATAL EXCEPTION: main
10-24 13:29:12.683: E/AndroidRuntime(440): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.mbijou.cosc473.ww3/edu.mbijou.cosc473.ww3.WW3Activity}: java.lang.NullPointerException
10-24 13:29:12.683: E/AndroidRuntime(440):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
10-24 13:29:12.683: E/AndroidRuntime(440):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
10-24 13:29:12.683: E/AndroidRuntime(440):  at android.app.ActivityThread.access$500(ActivityThread.java:122)
10-24 13:29:12.683: E/AndroidRuntime(440):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
10-24 13:29:12.683: E/AndroidRuntime(440):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 13:29:12.683: E/AndroidRuntime(440):  at android.os.Looper.loop(Looper.java:132)
10-24 13:29:12.683: E/AndroidRuntime(440):  at android.app.ActivityThread.main(ActivityThread.java:4123)
10-24 13:29:12.683: E/AndroidRuntime(440):  at java.lang.reflect.Method.invokeNative(Native Method)
10-24 13:29:12.683: E/AndroidRuntime(440):  at java.lang.reflect.Method.invoke(Method.java:491)
10-24 13:29:12.683: E/AndroidRuntime(440):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
10-24 13:29:12.683: E/AndroidRuntime(440):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
10-24 13:29:12.683: E/AndroidRuntime(440):  at dalvik.system.NativeStart.main(Native Method)
10-24 13:29:12.683: E/AndroidRuntime(440): Caused by: java.lang.NullPointerException
10-24 13:29:12.683: E/AndroidRuntime(440):  at edu.mbijou.cosc473.ww3.WW3Activity.onCreate(WW3Activity.java:38)
10-24 13:29:12.683: E/AndroidRuntime(440):  at android.app.Activity.performCreate(Activity.java:4397)
10-24 13:29:12.683: E/AndroidRuntime(440):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
10-24 13:29:12.683: E/AndroidRuntime(440):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
10-24 13:29:12.683: E/AndroidRuntime(440):  ... 11 more

startscreen.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="493dp"
        android:layout_marginTop="217dp"
        android:src="@drawable/logo" >
    </ImageView>
    <Button
        android:id="@+id/buttonNewGame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/logo"
        android:layout_marginLeft="200dp"
        android:layout_marginTop="133dp"
        android:clickable="true"
        android:text="@string/bNewGame" >
    </Button>
    <Button
        android:id="@+id/buttonLoadGame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/buttonNewGame"
        android:layout_alignLeft="@+id/buttonTroll"
        android:layout_alignRight="@+id/buttonTroll"
        android:clickable="true"
        android:text="@string/bLoadGame" >
    </Button>    <Button
        android:id="@+id/buttonExit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/buttonLoadGame"
        android:layout_alignBottom="@+id/buttonLoadGame"
        android:layout_alignLeft="@+id/buttonDebug"
        android:layout_alignRight="@+id/buttonDebug"
        android:layout_toRightOf="@+id/buttonLoadGame"
        android:clickable="true"
        android:text="@string/bExit" >
    </Button>
    <Button
        android:id="@+id/buttonAbout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/buttonNewGame"
        android:layout_alignRight="@+id/buttonNewGame"
        android:layout_below="@+id/buttonNewGame"
        android:layout_marginTop="50dp"
        android:text="@string/bAbout" />
    <Button
        android:id="@+id/buttonTroll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/buttonAbout"
        android:layout_alignBottom="@+id/buttonAbout"
        android:layout_centerHorizontal="true"
        android:text="@string/bTrollface" />
    <Button
        android:id="@+id/buttonDebug"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/buttonTroll"
        android:layout_toRightOf="@+id/buttonTroll"
        android:layout_marginLeft="200dp"
        android:text="@string/bDebug" />
</RelativeLayout>

我最初有一个 NullPointerException,因为我在将布局视图更改为 startscreen 之前声明了一组按钮,但我修复了该问题,然后发生了这种情况。从技术上讲,这是家庭作业,但我不认为它有必要贴上家庭作业标签,因为这不是家庭作业类型的问题,因为这个问题与实际作业本身无关。

I'm writing an Android game app for an Artificial Intelligence semester project. I'm getting a NullPointerException on Line 38 of the main class, which is the setOnClickListener for the new game button from the launch screen.

Relevant sections Class WW3Activity:

public class WW3Activity extends Activity
{
public boolean DebugMode = false;
private String buildMenuEmplacement = ""; 

//[Initialization] Called when the app is first launched
@Override
public void onCreate(Bundle savedInstanceState)
{           
    super.onCreate(savedInstanceState);
    setContentView(R.layout.startscreen);

    //[Initialization] Makes the button resources available to the class
    final Button newGame = (Button) findViewById(R.id.buttonNewGame);
    final Button loadGame = (Button) findViewById(R.id.buttonLoadGame);
    final Button exitGame = (Button) findViewById(R.id.buttonExit);
    final Button about = (Button) findViewById(R.id.buttonAbout);
    final Button troll = (Button) findViewById(R.id.buttonTroll);
    final Button debug = (Button) findViewById(R.id.buttonDebug);

    //[Action] When user pushes the New Game button
    newGame.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            // Perform action on click
            setContentView(R.layout.main);
            buttonInitialize();
        }
    });
    //[Action] When user pushes the Exit Game button
    exitGame.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            // Perform action on click
            finish();
        }
    });
    //[Action] When user pushes the Debug Mode button
    exitGame.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v)
        {
            // Perform action on click
            DebugMode = true;
        }


      });
    }

    //[Initialization] Gets the emplacements ready to be pushed
   public void buttonInitialize()
   {
        final ImageView pe1 = (ImageView) findViewById(R.id.playerEmplacement1);
        final ImageView pe2 = (ImageView) findViewById(R.id.playerEmplacement2);
        final ImageView pe3 = (ImageView) findViewById(R.id.playerEmplacement3);
        final ImageView pc = (ImageView) findViewById(R.id.playerCity);
        final ImageView ee1 = (ImageView) findViewById(R.id.enemyEmplacement1);
        final ImageView ee2 = (ImageView) findViewById(R.id.enemyEmplacement2);
        final ImageView ee3 = (ImageView) findViewById(R.id.enemyEmplacement3);
        final ImageView ec = (ImageView) findViewById(R.id.enemyCity);

        registerForContextMenu(findViewById(R.id.playerEmplacement1));
        registerForContextMenu(findViewById(R.id.playerEmplacement2));
        registerForContextMenu(findViewById(R.id.playerEmplacement3));
        registerForContextMenu(findViewById(R.id.enemyEmplacement1));
        registerForContextMenu(findViewById(R.id.enemyEmplacement2));
        registerForContextMenu(findViewById(R.id.enemyEmplacement3));

        /*empAction(pe1);
        empAction(pe2);
        empAction(pe3);
        empAction(ee1);
        empAction(ee2);
        empAction(ee3);
        empAction(ec);
        empAction(pc);*/
    }

And the LogCat:

10-24 13:29:12.297: D/dalvikvm(440): GC_FOR_ALLOC freed 40K, 4% free 6356K/6595K, paused 177ms
10-24 13:29:12.303: I/dalvikvm-heap(440): Grow heap (frag case) to 6.653MB for 409616-byte allocation
10-24 13:29:12.503: D/dalvikvm(440): GC_FOR_ALLOC freed <1K, 5% free 6755K/7047K, paused 128ms
10-24 13:29:12.614: D/dalvikvm(440): GC_FOR_ALLOC freed 400K, 8% free 6762K/7303K, paused 57ms
10-24 13:29:12.663: D/AndroidRuntime(440): Shutting down VM
10-24 13:29:12.663: W/dalvikvm(440): threadid=1: thread exiting with uncaught exception (group=0x40014760)
10-24 13:29:12.683: E/AndroidRuntime(440): FATAL EXCEPTION: main
10-24 13:29:12.683: E/AndroidRuntime(440): java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.mbijou.cosc473.ww3/edu.mbijou.cosc473.ww3.WW3Activity}: java.lang.NullPointerException
10-24 13:29:12.683: E/AndroidRuntime(440):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)
10-24 13:29:12.683: E/AndroidRuntime(440):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831)
10-24 13:29:12.683: E/AndroidRuntime(440):  at android.app.ActivityThread.access$500(ActivityThread.java:122)
10-24 13:29:12.683: E/AndroidRuntime(440):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024)
10-24 13:29:12.683: E/AndroidRuntime(440):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 13:29:12.683: E/AndroidRuntime(440):  at android.os.Looper.loop(Looper.java:132)
10-24 13:29:12.683: E/AndroidRuntime(440):  at android.app.ActivityThread.main(ActivityThread.java:4123)
10-24 13:29:12.683: E/AndroidRuntime(440):  at java.lang.reflect.Method.invokeNative(Native Method)
10-24 13:29:12.683: E/AndroidRuntime(440):  at java.lang.reflect.Method.invoke(Method.java:491)
10-24 13:29:12.683: E/AndroidRuntime(440):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
10-24 13:29:12.683: E/AndroidRuntime(440):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
10-24 13:29:12.683: E/AndroidRuntime(440):  at dalvik.system.NativeStart.main(Native Method)
10-24 13:29:12.683: E/AndroidRuntime(440): Caused by: java.lang.NullPointerException
10-24 13:29:12.683: E/AndroidRuntime(440):  at edu.mbijou.cosc473.ww3.WW3Activity.onCreate(WW3Activity.java:38)
10-24 13:29:12.683: E/AndroidRuntime(440):  at android.app.Activity.performCreate(Activity.java:4397)
10-24 13:29:12.683: E/AndroidRuntime(440):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
10-24 13:29:12.683: E/AndroidRuntime(440):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779)
10-24 13:29:12.683: E/AndroidRuntime(440):  ... 11 more

startscreen.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="493dp"
        android:layout_marginTop="217dp"
        android:src="@drawable/logo" >
    </ImageView>
    <Button
        android:id="@+id/buttonNewGame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/logo"
        android:layout_marginLeft="200dp"
        android:layout_marginTop="133dp"
        android:clickable="true"
        android:text="@string/bNewGame" >
    </Button>
    <Button
        android:id="@+id/buttonLoadGame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/buttonNewGame"
        android:layout_alignLeft="@+id/buttonTroll"
        android:layout_alignRight="@+id/buttonTroll"
        android:clickable="true"
        android:text="@string/bLoadGame" >
    </Button>    <Button
        android:id="@+id/buttonExit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/buttonLoadGame"
        android:layout_alignBottom="@+id/buttonLoadGame"
        android:layout_alignLeft="@+id/buttonDebug"
        android:layout_alignRight="@+id/buttonDebug"
        android:layout_toRightOf="@+id/buttonLoadGame"
        android:clickable="true"
        android:text="@string/bExit" >
    </Button>
    <Button
        android:id="@+id/buttonAbout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/buttonNewGame"
        android:layout_alignRight="@+id/buttonNewGame"
        android:layout_below="@+id/buttonNewGame"
        android:layout_marginTop="50dp"
        android:text="@string/bAbout" />
    <Button
        android:id="@+id/buttonTroll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/buttonAbout"
        android:layout_alignBottom="@+id/buttonAbout"
        android:layout_centerHorizontal="true"
        android:text="@string/bTrollface" />
    <Button
        android:id="@+id/buttonDebug"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/buttonTroll"
        android:layout_toRightOf="@+id/buttonTroll"
        android:layout_marginLeft="200dp"
        android:text="@string/bDebug" />
</RelativeLayout>

I originally had a NullPointerException because I declared the set of buttons before I changed the layout view to startscreen, but I fixed that, and then this happened. Technically this IS homework, but I don't think that it warrants the homework tag because this isn't a homework-y type of problem, because the problem has nothing to do with the actual assignment itself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝梦月影 2024-12-18 17:50:10

findViewById() 返回 null 的唯一原因是您正在查找的 View 不属于当前 View。 (您使用 setContentView() 设置的视图)。如果 R.id.buttonNewGame 位于 R.layout.startscreen 中,请尝试清理并重建您的项目。

The only reason why findViewById() would return null is because the View you are looking for does not belong to the current View. (The View you set with setContentView()). If R.id.buttonNewGame is in R.layout.startscreen, try to clean and rebuild your project.

鹿童谣 2024-12-18 17:50:10

看起来 newGame 为空。您是否有多个名为 startscreen.xml 的布局?导致此异常的一个常见原因是在一个版本中创建buttonNewGame,但在另一个版本中则不创建;当活动布局无法指定按钮时,这将导致代码正确构建,但在运行时失败,正如您所描述的那样。

It would seem that newGame is null. Do you have multiple layouts named startscreen.xml? A common cause of this exception would be creating buttonNewGame in one version but not the other; this would cause the code to build correctly but fail at runtime, exactly as you're describing, when the active layout failed to specify the button.

追风人 2024-12-18 17:50:10

我定期看到这个错误,我的情况的问题是 android 在不同的布局中不正确地维护相同的 id。确保您的 id 不仅在一个布局中而且在整个应用程序中都是唯一的。

I see this error periodically, the trouble in my case is that android inpropertly maintaining same id's in different layouts. Make sure that your id's are unique not inside only one layout, but in entire application.

反差帅 2024-12-18 17:50:10

在第 38 行设置断点并启动调试器。您很可能尝试引用 xml 文件 R.layout.startscreen 之外的按钮,这会引发 NullPointerException。

Set a break point on line 38 and launch the debugger. You're most likely trying to reference a button outside of the xml file R.layout.startscreen which would throw a NullPointerException.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文