我无法从我的主要活动切换到listactivity,我通过观看视频教程创建了listactivity并按照相同的步骤但是只要我点击按钮切换到菜单我就会收到错误“不幸的是,该计划已停止工作“
Button button1;
button1 = (Button)findViewById(R.id.but1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this,Menu.class);
startActivity(i);
Menu类如下:
public class Menu extends ListActivity{
String classes[] = {"First","Second","Third"};
@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.ashsoft.basiccprogram." + cheese);
Intent ourIntent=new Intent(Menu.this,ourClass);
startActivity(ourIntent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
Logcat:
Log file : `01-13 13:53:03.481: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:03.481: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:03.481: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:04.511: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:04.521: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:05.602: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:05.602: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:05.681: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:05.771: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:06.001: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:06.071: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:06.071: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:06.181: I/Choreographer(1400): Skipped 68 frames! The application may be doing too much work on its main thread.
01-13 13:53:06.362: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:06.442: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:06.491: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:06.501: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:06.511: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:06.532: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:06.581: D/gralloc_goldfish(1400): Emulator without GPU emulation detected.
01-13 13:53:06.702: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:06.851: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:16.741: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:16.782: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:16.801: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:16.801: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:16.872: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:16.911: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:16.931: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:16.951: W/Trace(1400): Unexpected value from nativeGetEnabledTags: 0
01-13 13:53:17.052: D/AndroidRuntime(1400): Shutting down VM
01-13 13:53:17.052: W/dalvikvm(1400): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
01-13 13:53:17.151: E/AndroidRuntime(1400): FATAL EXCEPTION: main
01-13 13:53:17.151: E/AndroidRuntime(1400): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.ashsoft.basiccprogram/android.view.Menu}; have you declared this activity in your AndroidManifest.xml?
01-13 13:53:17.151: E/AndroidRuntime(1400): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
01-13 13:53:17.151: E/AndroidRuntime(1400): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
01-13 13:53:17.151: E/AndroidRuntime(1400): at android.app.Activity.startActivityForResult(Activity.java:3370)
01-13 13:53:17.151: E/AndroidRuntime(1400): at android.app.Activity.startActivityForResult(Activity.java:3331)
01-13 13:53:17.151: E/AndroidRuntime(1400): at android.app.Activity.startActivity(Activity.java:3566)
01-13 13:53:17.151: E/AndroidRuntime(1400): at android.app.Activity.startActivity(Activity.java:3534)
01-13 13:53:17.151: E/AndroidRuntime(1400): at com.ashsoft.basiccprogram.MainActivity$1.onClick(MainActivity.java:27)
01-13 13:53:17.151: E/AndroidRuntime(1400): at android.view.View.performClick(View.java:4202)
01-13 13:53:17.151: E/AndroidRuntime(1400): at android.view.View$PerformClick.run(View.java:17340)
01-13 13:53:17.151: E/AndroidRuntime(1400): at android.os.Handler.handleCallback(Handler.java:725)
01-13 13:53:17.151: E/AndroidRuntime(1400): at android.os.Handler.dispatchMessage(Handler.java:92)
01-13 13:53:17.151: E/AndroidRuntime(1400): at android.os.Looper.loop(Looper.java:137)
01-13 13:53:17.151: E/AndroidRuntime(1400): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-13 13:53:17.151: E/AndroidRuntime(1400): at java.lang.reflect.Method.invokeNative(Native Method)
01-13 13:53:17.151: E/AndroidRuntime(1400): at java.lang.reflect.Method.invoke(Method.java:511)
01-13 13:53:17.151: E/AndroidRuntime(1400): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-13 13:53:17.151: E/AndroidRuntime(1400): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-13 13:53:17.151: E/AndroidRuntime(1400): at dalvik.system.NativeStart.main(Native Method)
01-13 13:53:25.911: I/Process(1400): Sending signal. PID: 1400 SIG: 9
`
清单代码:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.ashsoft.basiccprogram.MainActivity"
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="com.ashsoft.basiccprogram.First"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.FIRST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.ashsoft.basiccprogram.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>
</application>
我使用的教程没有使用任何xml文件作为列表。
主要问题是列表没有显示出来。每当我点击主要活动的菜单按钮时,它就会停止工作。
答案 0 :(得分:0)
您似乎尚未在清单文件中注册该Menu类。
如果它仍然不起作用,请发布logcat,以便有可能编辑我的答案。
修改强>
首先,你的漫长过程任务必须以asynctask为基础。
暂时让我们离开。试试这个:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.ashsoft.basiccprogram.MainActivity"
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="com.ashsoft.basiccprogram.First"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.FIRST" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".ListScreenActivity"
android:label="@string/app_name" >
</activity>
您的ListScreenActivity:
public class ListScreenActivity extends ListActivity{
String classes[] = {"First","Second","Third"};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(ListScreenActivity.this,android.R.layout.simple_list_item_1, classes));
ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
//show toast
Toast.makeText(getApplicationContext(),"Clicked on "+classes[position], Toast.LENGTH_SHORT).show();
}
});
}