我在尝试发起一个子活动时崩溃了。我有一个小应用程序来演示这个问题。这个应用程序的主要部分是一个列表视图,当你点击列表视图中的一个项目时,它应该会启动一个活动,从而打开一个图库视图。在为图库视图类调用onCreate()之前,应用程序崩溃,所以我怀疑我在活动描述的xml中省略了一些必要的东西。
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="randombrand.ListGallery"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:label="@string/app_name" android:name=".ListGallery">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:label="Manual Top" android:name=".TestGallery">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>java:
public class ListGallery extends ListActivity
{
private static final String[] astrMainMenu = { "List Item 1", "List Item 2" };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.main,
astrMainMenu));
ListView lView = getListView();
lView.setTextFilterEnabled(true);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
Intent intent = new Intent();
intent.setClass(this, TestGallery.class);
startActivity(intent);
}
}调用startActivity()时崩溃的堆栈跟踪:
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2417
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2512
ActivityThread.access$2200(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 119
ActivityThread$H.handleMessage(Message) line: 1863
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 4363
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 860
ZygoteInit.main(String[]) line: 618
NativeStart.main(String[]) line: not available [native method] 提前谢谢你,
杰伦
发布于 2011-06-21 03:12:24
您应该使用下面的代码创建Intent:
Intent intent = new Intent(this, TestGallery.class);仅仅指定类名是不够的,您还需要指定这个类所在的包名。在上面的代码示例中,包名取自this (this为Context,包名通过Context.getPackageName()获取)。
P.S.请注意,在“包”下面,我指的不是java的包,而是Android的包。在AndroidManifest.xml文件中指定的值。
https://stackoverflow.com/questions/6416056
复制相似问题