Java android应用程序在停止一项活动并启动另一项活动时崩溃
我在Java android应用程序在停止一项活动并启动另一项活动时崩溃,java,android,eclipse,Java,Android,Eclipse,我在Scene2.java中设置了一个按钮。我想使用该按钮进入其他活动Scene3.java,GameOver.java一切正常,直到即将打开新活动,每次应用程序在那里崩溃。我想知道我在连接中是否犯了任何错误,我指的是Scene2.javaGameOver.java和Scene3.java Scene2.java package com.group5.littlered; import android.app.Activity; import android.app.AlertDialog;
Scene2.java
中设置了一个按钮。我想使用该按钮进入其他活动Scene3.java
,GameOver.java
一切正常,直到即将打开新活动,每次应用程序在那里崩溃。我想知道我在连接中是否犯了任何错误,我指的是Scene2.java
GameOver.java
和Scene3.java
Scene2.java
package com.group5.littlered;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class Scene2 extends Activity {
MediaPlayer bird;
MediaPlayer bgm;
int position = 0;
String[] conversation;
TextView frame;
ImageView conframe;
final String[] ListStr = { "Wake up and ask her", "Peek her secretly" };
int plot = 0;
@Override
public void onBackPressed() {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Remove title bar
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
// Remove notification bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_scene2);
Intent intent1 = getIntent();
conversation = getResources().getStringArray(R.array.scene2);
frame = (TextView) findViewById(R.id.textView1);
Button next = (Button) findViewById(R.id.wtf);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (position < 2) {
String sentence = conversation[position];
frame.setText(sentence + "");
position++;
} else {
if (plot < 1) {
AlertDialog choice = new AlertDialog.Builder(
Scene2.this).create();
choice.setTitle("Pick a choice");
choice.setMessage(" ");
choice.setButton("Get up and ask her what happened",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
plot = 1;
}
});
choice.setButton2("Peek her secretly",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// TODO Auto-generated method stub
plot = 2;
position = 4;
}
});
choice.show();
} else {
if (plot < 2) {
if (position < 4) {
String sentence = conversation[position];
frame.setText(sentence + "");
position++;
} else {
Intent intent2 = new Intent(Scene2.this,
GameOver.class);
startActivity(intent2);
finish();
}
} else {
if (position < 6) {
String sentence = conversation[position];
frame.setText(sentence + "");
position++;
} else {
Intent intent3 = new Intent(Scene2.this,
Scene3.class);
startActivity(intent3);
finish();
}
}
}
}
}
});
// BGM
bgm = MediaPlayer.create(Scene2.this, R.raw.voyager);
bgm.setLooping(true);
bgm.start();
// bird
bird = MediaPlayer.create(Scene2.this, R.raw.bird);
bird.setLooping(false);
bird.start();
}
}
检查清单文件并在其中添加Scene3.java
<activity
android:name=".Scene3" >
</activity>
始终发布问题,但有例外,其次可能是您在清单文件中未提及其他活动,如:
<activity
android:name=".Scene3">
</activity>
<activity
android:name=".GameOver">
</activity>
正在崩溃的行是场景3的第45行:
Button next = (Button) findViewById(R.id.wtf);
next.setOnClickListener(new View.OnClickListener() { // <-- THIS ONE
...
});
您必须重新访问此布局,以确保您愿意访问的按钮实际存在,ID为wtf
一般来说,在不同的布局中使用相同的ID是很危险的。这很容易隐藏错误,否则很容易被发现,因为这将无法编译。您是否已将Scene3和GameOver添加到清单中?请使用崩溃堆栈跟踪发布LogCat输出。您不应该在startActivity(intent2)
之后调用finish()
。活动是堆叠的,如果您想阻止用户从Scene3
或GameOver
返回,最好用另一种方式。这可能是坠机的原因。但我无法进一步调查,因为您没有发布与崩溃对应的堆栈跟踪。我将在一段时间后上载堆栈跟踪。请稍候。谢谢我上载了我的日志猫,也许您可以检查一下。请只发布相关代码。我在清单中添加了这些代码。仍然不起作用。也许你可以查一下我的日志
<activity
android:name=".Scene3">
</activity>
<activity
android:name=".GameOver">
</activity>
Button next = (Button) findViewById(R.id.wtf);
next.setOnClickListener(new View.OnClickListener() { // <-- THIS ONE
...
});
setContentView(R.layout.activity_scene3); // and later on findViewById() returns `null`