无法理解为什么程序在代码中到达此部分时会崩溃

时间:2013-04-09 15:38:09

标签: java android eclipse opengl-es imagebutton

当它到达“startBtn”

时似乎崩溃

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

以下是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

“start”和“exit”都是位于res / drawable文件夹中的.png文件

1 个答案:

答案 0 :(得分:4)

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

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

findViewById需要视图的ID才能引用它。

你应该使用

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