-2

似乎在到达“startBtn”时崩溃

我目前在 Eclipse 中使用带有 Android SDK 的 Java,非常感谢您的帮助

下面是 MainMenu 类的代码:

package com.pandagame;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.util.Log;

public class MainMenu extends Activity 
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        Log.d("MainMenu", "onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        /** Fire up background music */
        Log.d("MainMenu", "Fire up music");
        Engine.musicThread = new Thread()
        {   
            public void run()
            {
                Intent bgmusic = new
                Intent(getApplicationContext(), Music.class);
                startService(bgmusic);
                Engine.context = getApplicationContext();
            }
        };
        Log.d("MainMenu", "Engine.MusicThread");
        Engine.musicThread.start();
        Log.d("MainMenu", "new Engine");
        final Engine engine = new Engine();
        /** Set menu button options */
        Log.d("MainMenu", "Set Menu button options");
        ImageButton startBtn = (ImageButton)findViewById(R.drawable.start);
        Log.d("MainMenu", "Step 1");
        ImageButton exitBtn = (ImageButton)findViewById(R.drawable.exit);
        Log.d("MainMenu", "Step 2");
        startBtn.getBackground().setAlpha(Engine.MENU_BUTTON_ALPHA);
        Log.d("MainMenu", "Step 3");
        startBtn.setHapticFeedbackEnabled(Engine.HAPTIC_BUTTON_FEEDBACK);
        Log.d("MainMenu", "Step 4");
        exitBtn.getBackground().setAlpha(Engine.MENU_BUTTON_ALPHA);
        Log.d("MainMenu", "Step 5");
        exitBtn.setHapticFeedbackEnabled(Engine.HAPTIC_BUTTON_FEEDBACK);
        Log.d("MainMenu", "Step 6");
        startBtn.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                Log.d("MainMenu", "onClick");
                /** Start Game!!!! */
                Intent game = new Intent(getApplicationContext(),Game.class);
                MainMenu.this.startActivity(game);

            }
        });

        exitBtn.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                boolean clean = false;
                engine.onExit(v);
                if (clean)
                {
                    int pid= android.os.Process.myPid();
                    android.os.Process.killProcess(pid);
                }
            }
        });

    }
}

其他课程包括:

Background.java
Game.java
Engine.java
Music.java
PandaActivity.java
Screen.java

“开始”和“退出”都是位于 res/drawable 文件夹中的 .png 文件

4

1 回答 1

4

您正在尝试使用可绘制 id 引用 ImageButtons

IE

 ImageButton startBtn = (ImageButton)findViewById(R.drawable.start);

findViewById 需要视图的 id 来引用它。

你应该使用

 ImageButton startBtn = (ImageButton)findViewById(R.id.start);
于 2013-04-09T15:44:33.077 回答