我是Android应用程序开发的新手,并使用The New Boston教程。在第21课,当我运行我的代码时,一切都很好但是当我点击列表时没有运行的活动! 。 请帮忙。我检查了一切,但我无法理解:(。
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test1.zip.com"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<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:name=".Menu"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.zip.test1.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Action"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.zip.test1.ACTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Test1Activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.zip.test1.TEST1ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Menu.java
package test1.zip.com;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Menu extends ListActivity {
String classes[] = { "Test1Activity", "Action", "Test1", "Test2" };
@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.zip.test1." + cheese);
Intent ourIntent = new Intent(Menu.this, ourclass);
startActivity(ourIntent);
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
logcat的
11-03 05:52:24.744: D/AndroidRuntime(7191): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
11-03 05:52:24.744: D/AndroidRuntime(7191): CheckJNI is OFF
11-03 05:52:24.920: D/AndroidRuntime(7191): Calling main entry com.android.commands.pm.Pm
11-03 05:52:24.924: D/AndroidRuntime(7191): Shutting down VM
11-03 05:52:24.936: D/dalvikvm(7191): GC_CONCURRENT freed 99K, 72% free 294K/1024K, external 0K/0K, paused 0ms+2ms
11-03 05:52:24.936: D/dalvikvm(7191): Debugger has detached; object registry had 1 entries
11-03 05:52:24.948: I/AndroidRuntime(7191): NOTE: attach of thread 'Binder Thread #1' failed
11-03 05:52:25.148: D/AndroidRuntime(7198): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
11-03 05:52:25.148: D/AndroidRuntime(7198): CheckJNI is OFF
11-03 05:52:25.349: D/AndroidRuntime(7198): Calling main entry com.android.commands.am.Am
11-03 05:52:25.356: I/ActivityManager(157): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=test1.zip.com/.Splash } from pid 7198
11-03 05:52:25.361: D/AndroidRuntime(7198): Shutting down VM
11-03 05:52:25.368: D/dalvikvm(7198): GC_CONCURRENT freed 100K, 70% free 316K/1024K, external 0K/0K, paused 0ms+3ms
11-03 05:52:25.372: D/dalvikvm(7198): Debugger has detached; object registry had 1 entries
11-03 05:52:25.380: I/AndroidRuntime(7198): NOTE: attach of thread 'Binder Thread #2' failed
11-03 05:52:33.905: W/System.err(7002): java.lang.ClassNotFoundException: com.zip.test1.Test1Activity
11-03 05:52:33.908: W/System.err(7002): at java.lang.Class.classForName(Native Method)
11-03 05:52:33.908: W/System.err(7002): at java.lang.Class.forName(Class.java:234)
11-03 05:52:33.908: W/System.err(7002): at java.lang.Class.forName(Class.java:181)
11-03 05:52:33.908: W/System.err(7002): at test1.zip.com.Menu.onListItemClick(Menu.java:33)
11-03 05:52:33.908: W/System.err(7002): at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
11-03 05:52:33.908: W/System.err(7002): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
11-03 05:52:33.908: W/System.err(7002): at android.widget.ListView.performItemClick(ListView.java:3513)
11-03 05:52:33.908: W/System.err(7002): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
11-03 05:52:33.908: W/System.err(7002): at android.os.Handler.handleCallback(Handler.java:587)
11-03 05:52:33.908: W/System.err(7002): at android.os.Handler.dispatchMessage(Handler.java:92)
11-03 05:52:33.908: W/System.err(7002): at android.os.Looper.loop(Looper.java:130)
11-03 05:52:33.908: W/System.err(7002): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-03 05:52:33.912: W/System.err(7002): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 05:52:33.912: W/System.err(7002): at java.lang.reflect.Method.invoke(Method.java:507)
11-03 05:52:33.912: W/System.err(7002): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-03 05:52:33.912: W/System.err(7002): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-03 05:52:33.912: W/System.err(7002): at dalvik.system.NativeStart.main(Native Method)
11-03 05:52:33.912: W/System.err(7002): Caused by: java.lang.NoClassDefFoundError: com.zip.test1.Test1Activity
11-03 05:52:33.912: W/System.err(7002): ... 17 more
11-03 05:52:33.912: W/System.err(7002): Caused by: java.lang.ClassNotFoundException: com.zip.test1.Test1Activity in loader dalvik.system.PathClassLoader[/data/app/test1.zip.com-2.apk]
11-03 05:52:33.916: W/System.err(7002): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
11-03 05:52:33.916: W/System.err(7002): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
11-03 05:52:33.916: W/System.err(7002): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
11-03 05:52:33.916: W/System.err(7002): ... 17 more
Test1Activity.java package test1.zip.com;
import android.app.Activity;
import android.os.Bundle;
public class Test1Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
我认为问题出在本部分
try {
Class ourclass = Class.forName("com.zip.test1." + cheese);
Intent ourIntent = new Intent(Menu.this, ourclass);
startActivity(ourIntent);
}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我更改为此并单击列表工作!但当然只是去行动课。
Intent ourIntent = new Intent("com.zip.test1.ACTION");
startActivity(ourIntent);
答案 0 :(得分:0)
尝试将classes数组中的所有String更改为大写。 Aslo在Manifest文件中声明所有活动,Test1和Test2都缺失。
答案 1 :(得分:0)
像这样投了类
Class<?> c = null; //<= this is what i say
if(StringClassname != null) {
try {
c = Class.forName(StringClassname );
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Intent intent = new Intent(mail.this, c);
startActivity(intent);
答案 2 :(得分:0)
好的,我得到了这个。你的清单文件就是你打电话给包裹。
package="test1.zip.com"
但是,在您尝试呼叫的清单的活动部分
<intent-filter>
<action android:name="com.zip.test1.MENU" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
因此,如果您将所有意图过滤器更改为以
开头 "test1.zip.com"
而不是
"com.zip.test1"
它将被修复。