2014-09-11 27 views
1

我跟着幾個教程,我發現這個錯誤未解決。 這是我的代碼。我的應用程序每次運行2秒後都會意外停止(預設定時器)應用程序意外停止Android開發

錯誤是:應用程序Test(process com.juicy.test)已意外停止。請再試一次。它會提示AVD。

W/dalvikvm(281):線程ID = 7:螺紋與未捕獲的異常 離開(組= 0x4001d800)

E/AndroidRuntime(281):致命異常:螺紋8

ë/AndroidRuntime(281):android.content.ActivityNotFoundException:否 活動發現處理意圖{行爲= com.juicy.test.MAINACTIVITY}

E/AndroidRuntime(281):在 android.app.Instrumentation.checkStartActivityResult (儀器廠entation.java:1408)

E/AndroidRuntime(281):在 android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)

E/AndroidRuntime(281):在 android.app。 Activity.startActivityForResult(Activity.java:2817)

E/AndroidRuntime(281):在 android.app.Activity.startActivity(Activity.java:2923)

E/AndroidRuntime(281):在玉米.juicy.test.Tree $ 1.run(Tree.java:24)**

Tree.xml

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tree); 

    Thread timer = new Thread(){ 
     public void run(){ 
      try{ 
       sleep(2000); 
      }catch(InterruptedException e){ 
       e.printStackTrace(); 
      } 
      finally{ 
       Intent openMainActivity = new Intent("com.juicy.test.MAINACTIVITY"); 
       startActivity(openMainActivity); 
      } 
     } 
    }; 
    timer.start(); 

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.juicy.test" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="21" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".Tree" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="com.juicy.test.MAINACTIVITY" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

MainActivity.java

package com.juicy.test; 


import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 


public class MainActivity extends ActionBarActivity { 

    int counter; 
    Button add, sub; 
    TextView display; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     counter = 0; 
     add = (Button) findViewById(R.id.bAdd); 
     sub = (Button) findViewById(R.id.bSub); 
     display = (TextView) findViewById(R.id.tvDisplay); 

     add.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       counter++; 
       display.setText("Your total is " + counter); 
      } 
     }); 
     sub.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       counter--; 
       display.setText("Your total is " + counter); 

      } 
     }); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 
+3

安置自己的錯誤日誌 – 2014-09-11 10:55:49

+3

張貼您的logcat,以便我們可能知道錯誤在哪裏... – Umair 2014-09-11 10:56:01

+0

對不起,我已經將這些logcat錯誤添加到我的文章。 – Danzeeeee 2014-09-11 11:05:41

回答

0

在您的清單文件,你在某時刻起2個活動,或者刪除從.MainActivity或.TREE活動這個代碼..

<intent-filter> 
      <action android:name="com.juicy.test.MAINACTIVITY" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 

希望這有助於...

+0

非常感謝!將1更改爲DEFAULT並且它可以工作。 – Danzeeeee 2014-09-11 11:14:14

+0

@Danzeeeee是的,它也可以這樣工作...... :) – Umair 2014-09-11 11:15:09

0

在你的清單文件已經在清單文件中設置了兩個啓動器。這裏啓動器意味着首先啓動哪個活動,因此只能有一個啓動器。

無論何時您啓動應用程序,它都會從清單頂部搜索包含啓動程序的活動,並打開包含啓動程序(您的情況爲.Tree)的第一個活動。請記得設置其他活動以外的啓動器類別。

在AndroidManifest.xml中,將包含MainActivity的應用程序更改爲android.intent.category。DEFAULT

<activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="com.juicy.test.MAINACTIVITY" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 

注:因爲當你DONOT指定任何類別

+0

非常感謝,它適合我! :) – Danzeeeee 2014-09-11 11:13:54

0

打開清單和Ctrl點擊活動類名稱設置爲默認你也可以刪除類別字段。如果類文件沒有打開,機會是,它沒有正確映射。這可能會導致此錯誤。

+0

解決,謝謝btw :) – Danzeeeee 2014-09-11 11:14:37