Java 无法理解程序到达代码中此部分时为何崩溃
似乎在它到达“startBtn”时就会崩溃 我目前正在Eclipse中使用Java和Android SDK,非常感谢您的帮助 以下是MainMenu类的代码:Java 无法理解程序到达代码中此部分时为何崩溃,java,android,eclipse,opengl-es,imagebutton,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.OnClickLi
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
“开始”和“退出”都是.png文件,它们位于res/drawable文件夹中您试图引用具有drawable id的ImageButtons i、 e
findViewById
需要视图的id来引用它
你应该使用
ImageButton startBtn = (ImageButton)findViewById(R.id.start);
发布logcat pleasehmmm,使用您在xml布局中添加的ImageButton的id,而不是drawable。将imagebutton查找为
imagebutton startBtn=(imagebutton)findViewById(R.id.start)代码>-1用于无StackTrace的崩溃我已尝试使用ImageButton startBtn=(ImageButton)findViewById(R.id.start);但这只是一个错误。(R.drawable.start)的“开始”部分是一个.png文件,它引用的是一幅图片。当它编译时,它也会通过这个部分。当它点击startBtn.getBackground().setAlpha(引擎菜单按钮α)时崩溃;我已经试过你的ImageButton startBtn=(ImageButton)findViewById(R.id.start);但这只是一个错误。您应该知道(R.drawable.start)的“开始”部分是一个.png文件,指的是一幅图片。而且,当它编译时,它确实通过了这个部分。当它点击startBtn.getBackground().setAlpha(引擎菜单按钮α)时崩溃;
ImageButton startBtn = (ImageButton)findViewById(R.id.start);