我的应用程序应该在按钮单击时将活动更改为其他活动,但它会崩溃。我有两个.java文件和两个.xml文件。 以下是红色错误消息。谢谢
09-28 01:16:35.020: D/AndroidRuntime(1606): Shutting down VM
09-28 01:16:35.020: W/dalvikvm(1606): threadid=1: thread exiting with uncaught exception (group=0xb3b04ba8)
09-28 01:16:35.090: E/AndroidRuntime(1606): FATAL EXCEPTION: main
09-28 01:16:35.090: E/AndroidRuntime(1606): Process: com.example.startscreenapp, PID: 1606
09-28 01:16:35.090: E/AndroidRuntime(1606): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.startscreenapp/com.example.startscreenapp.PlayGame}; have you declared this activity in your AndroidManifest.xml?
09-28 01:16:35.090: E/AndroidRuntime(1606): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
09-28 01:16:35.090: E/AndroidRuntime(1606): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
09-28 01:16:35.090: E/AndroidRuntime(1606): at android.app.Activity.startActivityForResult(Activity.java:3424)
09-28 01:16:35.090: E/AndroidRuntime(1606): at android.app.Activity.startActivityForResult(Activity.java:3385)
09-28 01:16:35.090: E/AndroidRuntime(1606): at com.example.startscreenapp.StartScreen.startGame(StartScreen.java:46)
09-28 01:16:35.090: E/AndroidRuntime(1606): at com.example.startscreenapp.StartScreen.access$0(StartScreen.java:41)
09-28 01:16:35.090: E/AndroidRuntime(1606): at com.example.startscreenapp.StartScreen$1.onClick(StartScreen.java:26)
09-28 01:16:35.090: E/AndroidRuntime(1606): at android.view.View.performClick(View.java:4438)
09-28 01:16:35.090: E/AndroidRuntime(1606): at android.view.View$PerformClick.run(View.java:18422)
09-28 01:16:35.090: E/AndroidRuntime(1606): at android.os.Handler.handleCallback(Handler.java:733)
09-28 01:16:35.090: E/AndroidRuntime(1606): at android.os.Handler.dispatchMessage(Handler.java:95)
09-28 01:16:35.090: E/AndroidRuntime(1606): at android.os.Looper.loop(Looper.java:136)
09-28 01:16:35.090: E/AndroidRuntime(1606): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-28 01:16:35.090: E/AndroidRuntime(1606): at java.lang.reflect.Method.invokeNative(Native Method)
09-28 01:16:35.090: E/AndroidRuntime(1606): at java.lang.reflect.Method.invoke(Method.java:515)
09-28 01:16:35.090: E/AndroidRuntime(1606): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-28 01:16:35.090: E/AndroidRuntime(1606): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-28 01:16:35.090: E/AndroidRuntime(1606): at dalvik.system.NativeStart.main(Native Method)
09-28 01:21:35.230: I/Process(1606): Sending signal. PID: 1606 SIG: 9
我有两个.java文件和两个.xml文件。
StartScreen.java
package com.example.startscreenapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class StartScreen extends Activity {
private static final int PLAY_GAME = 1010;
private TextView tv;
private int meaningOfLife = 42;
private String userName = "Douglas Adams";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.startscreen);
//Display initial values
tv.setText(userName + ":" + meaningOfLife);
//Set up button listener
Button startButton = (Button) findViewById(R.id.play_game);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startGame();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLAY_GAME && resultCode == RESULT_OK) {
meaningOfLife = data.getExtras().getInt("returnInt");
userName = data.getExtras().getString("returnStr");
//Show it has changed
tv.setText(userName + ":" + meaningOfLife);
}
super.onActivityResult(requestCode, resultCode, data);
}
private void startGame() {
Intent launchGame = new Intent(this, PlayGame.class);
//passing information to launched activity
launchGame.putExtra("meaningOfLife", meaningOfLife);
launchGame.putExtra("userName", userName);
startActivityForResult(launchGame, PLAY_GAME);
}
}
PlayGame.java
package com.example.startscreenapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class PlayGame extends Activity {
private TextView tv2;
int answer;
String author;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
tv2 = (TextView) findViewById(R.id.game_text);
//reading information passed to this activity
//Get the intent that started this activity
Intent i = getIntent();
//returns -1 if not initialized by calling activity
answer = i.getIntExtra("meaningOfLife", -1);
//returns [] if not initialized by calling activity
author = i.getStringExtra("userName");
tv2.setText(author + ":" + answer);
//Change values for an example of return
answer = answer - 41;
author = author + " Jr.";
//Set up button listener
Button startButton = (Button) findViewById(R.id.end_game);
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
//Return information to calling activity
Intent i = getIntent();
i.putExtra("returnInt", answer);
i.putExtra("returnStr", author);
setResult(RESULT_OK, i);
finish();
}
});
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="@+id/startscreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="115dp"
android:layout_marginTop="164dp"
android:text="TextView" />
<Button
android:id="@+id/play_game"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/startscreen"
android:layout_below="@+id/startscreen"
android:layout_marginTop="64dp"
android:text="Start" />
</RelativeLayout>
game.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/game_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/end_game"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
答案 0 :(得分:0)
您应该在AndroidManifest.xml
中声明所有活动,包括StartScreen
和PlayGame
。
例如,添加
<activity android:name="com.example.startscreenapp.PlayGame"></activity>
进入AndroidManifest.xml
答案 1 :(得分:0)
正如您的错误所说,您必须在清单中声明活动。
<activity
android:name="com.example.startscreenapp.PlayGame">
</activity>