开发者

Trying to start another Activity always only yields "ActivityNotFoundException". What am I missing?

开发者 https://www.devze.com 2023-01-13 21:04 出处:网络
I am programming an app that eventually will have several Activities. Right now, however, I got stuck trying to start the second activity from the first one. For some odd reason I am always only getti

I am programming an app that eventually will have several Activities. Right now, however, I got stuck trying to start the second activity from the first one. For some odd reason I am always only getting an ActivityNotFoundException.

The code that tries to start the second activity reads:

    ...
    Intent intent = new Intent(Intent.ACTION_INSERT);
     /* intent.addCategory("foo"); */
    Log.v(TAG, "starting activity: " + intent);             
    startActivity(intent);
    ...

The Intent.ACTION_INSERT string constant is "android.intent.action.INSERT".

The corresponding fragment in the AndroidManifest.xml reads:

    ...
    <activity 
        android:label="@string/item_details" 
        android:name="ItemDetails"
    开发者_JS百科    android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.INSERT" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="foo" />
        </intent-filter>
    </activity>
    ...

The Activity class "ItemDetails" exists and is in the same package as the "calling" Activity. The Intent names match and according to the Android docs the "android.intent.category.DEFAULT" category should apply to all Intents that have no category set. Still, that Activity is not found. Why?

I also tried to specify a unique category "foo" as shown in the commented line in the code snippet and also added that to the manifest file, but same result.... :-(

What am I missing? Any hints?


The android:name for your Activity should be the full path to the Activity, including the packages. Is that what you have?

Also, this may not be right for you, but for Activities that are only meant to be called from within your application you can just use an explicit intent:

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


Aaah! Finally I got the reason! I missed to add the package-prefix to the Activity's classname. I had thought that the package="..." attribute of the manifest tag would take care of that...

Sorry for the bandwidth then...

Michael

ADDED LATER: Thanks folks! You pointed it out. I saw your appends only later, though...


You need to specify the activity you want to start in the Intent.

intent.setClass(this, YourActivityClass.class);

Right now it looks like you're firing an Intent that doesn't have a target.

0

精彩评论

暂无评论...
验证码 换一张
取 消