我遵循了一些教程,但发现此错误尚未解决。
这是我的代码。 2秒后每次运行,我的应用程序都意外停止(预设计时器)

错误是:应用程序Test(process com.juicy.test)已意外停止。请再试一遍。它在AVD上提示。


  W / dalvikvm(281):threadid = 7:线程退出且未捕获异常
  (组= 0x4001d800)
  
  E / AndroidRuntime(281):致命异常:线程8
  
  E / AndroidRuntime(281):android.content.ActivityNotFoundException:否
  发现可处理意图的活动{act = com.juicy.test.MAINACTIVITY}
  
  E / AndroidRuntime(281):在
  android.app.Instrumentation.checkStartActivityResult(Instrumentation.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):at com.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);
    }
}

最佳答案

在清单文件中,您一次要启动两个活动,因此请从.MainActivity或.Tree活动。中删除此代码。

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

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


希望这可以帮助 ...

关于java - 应用程序意外停止Android开发,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25785598/

10-12 04:03