gpt4 book ai didi

java - Android 开发应用程序意外停止

转载 作者:太空宇宙 更新时间:2023-11-04 14:34:17 25 4
gpt4 key购买 nike

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

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

W/dalvikvm(281): threadid=7: thread exiting with uncaught exception (group=0x4001d800)

E/AndroidRuntime(281): FATAL EXCEPTION: Thread-8

E/AndroidRuntime(281): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.juicy.test.MAINACTIVITY }

E/AndroidRuntime(281): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)

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

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

E/AndroidRuntime(281): at 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);
}
}

最佳答案

在 list 文件中,您一次启动两个 Activity ,因此请从 .MainActivity 或 .Tree Activity 中删除此代码..

<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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com