2012-04-07 182 views
0

因此,無論何時點擊啓動頁面上的按鈕,它都會給我一個強制關閉錯誤。這裏是main.xml佈局文件的類:按下按鈕時強制關閉應用程序錯誤

public class ForeverAloneActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Button btnstrt = (Button) findViewById(R.id.toq1); 
     btnstrt.setOnClickListener(new View.OnClickListener(){ 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Intent frstq = new Intent(v.getContext(), QuestionOne.class); 
       startActivityForResult(frstq, 0); 

而這正是我認爲會產生錯誤的原因。這個類是有關頁面,在按下啓動頁面上的按鈕時,你會被帶到:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView (R.layout.frstq); 
    Button startQ2 = (Button) findViewById(R.id.toq2); 
    startQ2.setOnClickListener(new View.OnClickListener(){ 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent toQ2 = new Intent(v.getContext(), QuestionTwo.class); 
      final EditText number = (EditText) findViewById(R.id.editText1); 
      final Toast error = Toast.makeText(QuestionOne.this, "Please insert a value", Toast.LENGTH_SHORT); 
      if (number.getText().toString().equals("")) {error.show(); 
      }else{ 
      startActivityForResult(toQ2, 0);} 

這if語句是那裏的下一個頁面上,有一個EditText框。我試圖這樣做,如果EditText框中沒有任何內容,它會顯示一條「請插入一個值」的Toast消息。在將整數放入EditText框之前,該按鈕將不起作用。

如果有人可以幫忙,這將是非常感激。

的logcat:

04-07 19:33:58.199: W/dalvikvm(362): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
04-07 19:33:58.221: E/AndroidRuntime(362): FATAL EXCEPTION: main 
04-07 19:33:58.221: E/AndroidRuntime(362): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.kenning.foreveralone/com.kenning.foreveralone.QuestionOne}; have you declared this activity in your AndroidManifest.xml? 
04-07 19:33:58.221: E/AndroidRuntime(362): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404) 
04-07 19:33:58.221: E/AndroidRuntime(362): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378) 
04-07 19:33:58.221: E/AndroidRuntime(362): at android.app.Activity.startActivityForResult(Activity.java:2817) 
04-07 19:33:58.221: E/AndroidRuntime(362): at com.kenning.foreveralone.ForeverAloneActivity$1.onClick(ForeverAloneActivity.java:22) 
04-07 19:33:58.221: E/AndroidRuntime(362): at android.view.View.performClick(View.java:2408) 
04-07 19:33:58.221: E/AndroidRuntime(362): at android.view.View$PerformClick.run(View.java:8816) 
04-07 19:33:58.221: E/AndroidRuntime(362): at android.os.Handler.handleCallback(Handler.java:587) 
04-07 19:33:58.221: E/AndroidRuntime(362): at android.os.Handler.dispatchMessage(Handler.java:92) 
04-07 19:33:58.221: E/AndroidRuntime(362): at android.os.Looper.loop(Looper.java:123) 
04-07 19:33:58.221: E/AndroidRuntime(362): at android.app.ActivityThread.main(ActivityThread.java:4627) 
04-07 19:33:58.221: E/AndroidRuntime(362): at java.lang.reflect.Method.invokeNative(Native Method) 
04-07 19:33:58.221: E/AndroidRuntime(362): at java.lang.reflect.Method.invoke(Method.java:521) 
04-07 19:33:58.221: E/AndroidRuntime(362): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
04-07 19:33:58.221: E/AndroidRuntime(362): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
04-07 19:33:58.221: E/AndroidRuntime(362): at dalvik.system.NativeStart.main(Native Method) 
+0

什麼是error.show(); – 2012-04-07 18:32:59

+0

如果EditText字段中沒有值,則顯示Toast的名稱。 – MiKenning 2012-04-07 18:35:59

+0

在清單文件中添加QuestionOne活動 – 2012-04-07 18:38:49

回答

1

要補充的manifiest文件QuestionTwo? 檢查你的錯誤 - 你是否在你的AndroidManifest.xml中聲明瞭這個活動?

+0

我仍然收到強制關閉錯誤 – MiKenning 2012-04-07 18:34:08

+0

這似乎是一個異常錯誤,我將如何去捕獲該錯誤? – MiKenning 2012-04-07 18:35:03

+0

看到你的logcat .... – 2012-04-07 18:35:22

0

聲明您在活動文件中的活動。看來你是forgetted

<activity android:name="QuestionTwo" /> 

也不要忘記在您manifeast文件中的每個活動

+0

謝謝:):D – MiKenning 2012-04-07 18:45:48

0

嘿有你添加的QuestionTwo類作爲menifest文件的活動。如果添加

Intent toQ2 = new Intent(YourCurrentActivity.this,QuestionTwo.class); 

那麼之後必須還沒有menifest.B'coz添加的活性,清楚地表明瞭日誌中找不到活動英寸您必須在清單文件中聲明該活動。

希望你能運行你的應用程序。