Crash launching a sub activity
I am crashing trying to launch a sub-activity. I have a small app to demonstrate the problem. The main part of the app is a list view, and when you click on an item in the list view it is supposed to launch an activity which brings up a gallery view. The app crashes before onCreate() is called for the gallery view class, so I suspect I have omitted some necessary thing in the xml for the activity description.
manifest:
<?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);
}
}
stack trace for crash when I call 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开发者_JS百科.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 860
ZygoteInit.main(String[]) line: 618
NativeStart.main(String[]) line: not available [native method]
thanks in advance,
Jay
You should create Intent
using next code:
Intent intent = new Intent(this, TestGallery.class);
Its not enough to just specify class name, your also need to specify package name where this class is located. In the above code example package name is taken from this
(this
is Context
, and package name is taken via Context.getPackageName()
).
P.S. Note that under "package" I do not mean java's package, I mean Android's package. The one that is specified in AndroidManifest.xml
file.
精彩评论