我在切换活动时遇到了一些麻烦。我认为它与我的viewflipper有关。每当我点击“开始游戏”按钮切换到游戏活动时,它就会抛出错误“由于未捕获的异常导致主线程退出”。任何人都可以帮我一把吗?我知道它肯定不是开始游戏活动及其布局,两者都太简单而不能失败。就像我说的,我认为它与我的viewflipper有关。更进一步,我想解释更多。活动以具有用户名,密码和登录edittexts /按钮的视图开始,然后,当接受用户和传递时,它将视图翻转到主菜单。从那里,你按开始游戏。
package com.login.test;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ViewFlipper;
public class Login extends Activity {
public EditText username, password;
public Button loginbutton, start, stats;
public ViewFlipper flip;
public String dbu, dbp;
final Handler handler = new Handler();
Timer t = new Timer();
public static final String KEEP_INFO = "remeberinfo";
public void startgame() {
Intent myIntent = new Intent(Login.this, Game.class);
startActivity(myIntent);
}
public void seescore() {
Intent myIntent2 = new Intent(Login.this, Score.class);
startActivity(myIntent2);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
username = (EditText) this.findViewById(R.id.username);
password = (EditText) this.findViewById(R.id.password);
loginbutton = (Button) this.findViewById(R.id.loginbutton);
start = (Button) this.findViewById(R.id.start);
stats = (Button) this.findViewById(R.id.stats);
flip = (ViewFlipper) this.findViewById(R.id.flip);
//when a view is displayed
flip.setInAnimation(this,android.R.anim.fade_in);
//when a view disappears
flip.setOutAnimation(this, android.R.anim.fade_out);
final Toast accessdenied = Toast.makeText(getApplicationContext(), "Incorrect username/password", 3);
final Toast accessgranted = Toast.makeText(getApplicationContext(), "Logging in", 3);
//calling for a stored data byte using sharedpreferences
SharedPreferences settings = getSharedPreferences(KEEP_INFO,0);
String keptusername = settings.getString("keptuser", "");
String keptpassword = settings.getString("keptpass", "");
username.setText(keptusername);
password.setText(keptpassword);
loginbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dbu = (username.getText()).toString();
dbp = (password.getText()).toString();
try{
}
finally{
if ((dbu.equals("crete"))&&(dbp.equals("monee"))){
username.setBackgroundResource(R.drawable.greenloginbutton);
password.setBackgroundResource(R.drawable.greenloginbutton);
loginbutton.setBackgroundResource(R.drawable.greenloginbutton);
accessgranted.show();
//setting and valuing a data byte using sharedpreferences
SharedPreferences info = getSharedPreferences(KEEP_INFO,0);
SharedPreferences.Editor editor = info.edit();
editor.putString("keptuser", dbu);
editor.putString("keptpass", dbp);
editor.commit();
t.schedule(new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
flip.showNext();
}
});
}
}, 2000);
}
else {
t.schedule(new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
username.setBackgroundResource(R.drawable.loginbutton);
password.setBackgroundResource(R.drawable.loginbutton);
loginbutton.setBackgroundResource(R.drawable.loginbutton);
}
});
}
}, 3000);
accessdenied.show();
username.setBackgroundResource(R.drawable.redloginbutton);
password.setBackgroundResource(R.drawable.redloginbutton);
loginbutton.setBackgroundResource(R.drawable.redloginbutton);
}
}
}
});
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startgame();
}
});
stats.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
seescore();
}
});
}
}
LOG CAT
01-28 12:03:56.125: D/dalvikvm(322): GC freed 594 objects / 48504 bytes in 172ms
01-28 12:03:57.065: D/dalvikvm(322): GC freed 49 objects / 1904 bytes in 131ms
01-28 12:03:57.838: D/ViewFlipper(322): updateRunning() mVisible=true, mStarted=false, mUserPresent=true, mRunning=false
01-28 12:04:09.176: D/AndroidRuntime(322): Shutting down VM
01-28 12:04:09.185: W/dalvikvm(322): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
01-28 12:04:09.195: E/AndroidRuntime(322): Uncaught handler: thread main exiting due to uncaught exception
01-28 12:04:09.267: E/AndroidRuntime(322): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.beeseries.contextclues/com.beeseries.contextclues.Game}; have you declared this activity in your AndroidManifest.xml?
01-28 12:04:09.267: E/AndroidRuntime(322): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
01-28 12:04:09.267: E/AndroidRuntime(322): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
01-28 12:04:09.267: E/AndroidRuntime(322): at android.app.Activity.startActivityForResult(Activity.java:2749)
01-28 12:04:09.267: E/AndroidRuntime(322): at android.app.Activity.startActivity(Activity.java:2855)
01-28 12:04:09.267: E/AndroidRuntime(322): at com.beeseries.contextclues.Login.startgame(Login.java:29)
01-28 12:04:09.267: E/AndroidRuntime(322): at com.beeseries.contextclues.Login$2.onClick(Login.java:136)
01-28 12:04:09.267: E/AndroidRuntime(322): at android.view.View.performClick(View.java:2364)
01-28 12:04:09.267: E/AndroidRuntime(322): at android.view.View.onTouchEvent(View.java:4179)
01-28 12:04:09.267: E/AndroidRuntime(322): at android.widget.TextView.onTouchEvent(TextView.java:6540)
01-28 12:04:09.267: E/AndroidRuntime(322): at android.view.View.dispatchTouchEvent(View.java:3709)
01-28 12:04:09.267: E/AndroidRuntime(322): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-28 12:04:09.267: E/AndroidRuntime(322): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-28 12:04:09.267: E/AndroidRuntime(322): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-28 12:04:09.267: E/AndroidRuntime(322): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-28 12:04:09.267: E/AndroidRuntime(322): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-28 12:04:09.267: E/AndroidRuntime(322): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
01-28 12:04:09.267: E/AndroidRuntime(322): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
01-28 12:04:09.267: E/AndroidRuntime(322): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
01-28 12:04:09.267: E/AndroidRuntime(322): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
01-28 12:04:09.267: E/AndroidRuntime(322): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
01-28 12:04:09.267: E/AndroidRuntime(322): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
01-28 12:04:09.267: E/AndroidRuntime(322): at android.os.Handler.dispatchMessage(Handler.java:99)
01-28 12:04:09.267: E/AndroidRuntime(322): at android.os.Looper.loop(Looper.java:123)
01-28 12:04:09.267: E/AndroidRuntime(322): at android.app.ActivityThread.main(ActivityThread.java:4363)
01-28 12:04:09.267: E/AndroidRuntime(322): at java.lang.reflect.Method.invokeNative(Native Method)
01-28 12:04:09.267: E/AndroidRuntime(322): at java.lang.reflect.Method.invoke(Method.java:521)
01-28 12:04:09.267: E/AndroidRuntime(322): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-28 12:04:09.267: E/AndroidRuntime(322): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-28 12:04:09.267: E/AndroidRuntime(322): at dalvik.system.NativeStart.main(Native Method)
01-28 12:04:09.315: I/dalvikvm(322): threadid=7: reacting to signal 3
01-28 12:04:09.435: I/dalvikvm(322): Wrote stack trace to '/data/anr/traces.txt'
01-28 12:04:12.485: I/Process(322): Sending signal. PID: 322 SIG: 9
答案 0 :(得分:1)
您可能没有在AndroidManifest.xml
中声明您的游戏活动,如下所示:
<activity android:name=".Game"
<!-- other attributes and also possible intent-filters.-->
/>