gpt4 book ai didi

java - 使用 ListActivity 时强制关闭应用程序和 ActivityNotFoundException

转载 作者:行者123 更新时间:2023-11-30 11:36:12 25 4
gpt4 key购买 nike

我正在按照 YouTube 教程创建 Android 应用程序。在其中一个教程中,他们介绍了如何使用 List Activity 创建 Menu 类。

public class Menu extends ListActivity {

String classes[] = {"Start","example1","example2","example3","example4","example5","example6"};


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes));
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cheese = classes[position];
try{
Class ourClass = Class.forName("com.example.add." + cheese);
Intent ourIntent = new Intent(Menu.this,ourClass);
startActivity(ourIntent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}

并在 list 中添加 Activity 。

<activity
android:name=".Splash"
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:exported="false"
android:name=".Menu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MENU" />

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

<activity
android:exported="false"
android:name="com.example.add.Start"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.example.add.START" />

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

我的项目有一个 Start 类,它向计数器加一并显示它,定义背景图像、音乐的 Splash 类,它是主类。在添加 Menu 类之前,我的应用程序运行良好,但在我添加它之后,我的应用程序开始强制关闭并且 logcat 在第 28 行的 Splash 类中显示 ActivityNotFoundException 和错误

public class Splash extends Activity{

MediaPlayer ourSong;

@Override
protected void onCreate(Bundle Dab) {
// TODO Auto-generated method stub
super.onCreate(Dab);
setContentView(R.layout.splash);
ourSong = MediaPlayer.create(Splash.this, R.raw.nodebeat);
ourSong.start();
Thread timer = new Thread(){
public void run(){ //framework to start a new thread
try{
sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent openStart = new Intent("com.example.add.MENU");
startActivity(openStart);
}
}
};
timer.start();
}

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
ourSong.release();
finish();
}

所以请帮助我,我是 Android 应用程序开发的新手,我对这里很感兴趣。Logcat 日志的链接:- https://docs.google.com/file/d/0BxLaXhL-q50-MHlhUW93SmxNT0U/edit调试日志 https://docs.google.com/file/d/0BxLaXhL-q50-b1pzbVpIcVNQR2s/edit错误日志

谢谢。

最佳答案

您对要启动的内容使用了错误的 Intent 过滤器。所以要么改变

 <action android:name="android.intent.action.MENU" />

 <action android:name="com.example.add.MENU" />

所以一切都匹配。

或者在您的代码中更改您的调用:

Intent openStart = new Intent("android.intent.action.MENU");

你应该阅读 Intent Filters .

您也可以使用显式 Intent ,因为您没有导出此 Activity 并且您知道它的名称,所以我认为不需要 Intent 过滤器。

Intent openStart = new Intent(Splash.this, Menu.class);
startActivity(openStart);

关于java - 使用 ListActivity 时强制关闭应用程序和 ActivityNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14630499/

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