崩溃启动子活动

时间:2011-06-20 19:01:55

标签: android android-activity

我正在尝试启动子活动。我有一个小应用程序来演示这个问题。应用程序的主要部分是列表视图,当您单击列表视图中的项目时,它应该启动一个活动,该活动将显示库视图。在为库视图类调用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]  

提前感谢,

1 个答案:

答案 0 :(得分:-1)

您应该使用下一个代码创建Intent

Intent intent = new Intent(this, TestGallery.class);

仅仅指定类名是不够的,还需要指定此类所在的包名。在上面的代码示例中,包名称取自thisthisContext,包名称来自Context.getPackageName())。

P.S。请注意,在“package”下我不是指java的包,我的意思是Android的包。 AndroidManifest.xml文件中指定的那个。