Intent 选择器找不到要处理的应用

Intent chooser can't find app to handle

这是一个非常基本的问题,但我无法弄清楚。我想制作一个自定义的意图选择器。当我 运行 选择器意图时它崩溃了。当我 运行 与 ACTION_VIEW 的意图时,它完美地工作。它向我展示了来自模拟器的系统内置选择器,当我选择浏览器时,它会打开 Uri。

错误消息:

09-11 11:06:30.057    6070-6070/? D/dalvikvm﹕ Not late-enabling CheckJNI   (already on)
09-11 11:06:30.088    6070-6070/course.labs.intentslab D/dalvikvm﹕       GC_FOR_ALLOC freed 164K, 18% free 2795K/3404K, paused 1ms, total 3ms
09-11 11:06:30.138    6070-6070/course.labs.intentslab D/libEGL﹕ loaded    /system/lib/egl/libEGL_emulation.so
09-11 11:06:30.138    6070-6070/course.labs.intentslab D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_emulation.so
09-11 11:06:30.138    6070-6070/course.labs.intentslab D/libEGL﹕ loaded /system/lib/egl/libGLESv2_emulation.so
09-11 11:06:30.138    6070-6070/course.labs.intentslab D/﹕ HostConnection::get() New Host Connection established 0xb9579ef0, tid 6070
09-11 11:06:30.189    6070-6070/course.labs.intentslab W/EGL_emulation﹕ eglSurfaceAttrib not implemented
09-11 11:06:30.189    6070-6070/course.labs.intentslab D/OpenGLRenderer﹕ Enabling debug mode 0
09-11 11:06:31.807    6070-6070/course.labs.intentslab I/Lab-Intents﹕ Entered startImplicitActivation()
09-11 11:06:31.807    6070-6070/course.labs.intentslab I/Lab-Intents﹕ Chooser Intent Action:android.intent.action.CHOOSER
09-11 11:06:31.988    6070-6070/course.labs.intentslab W/ChooseActivity﹕ Target is not an intent: null
09-11 11:06:31.988    6070-6070/course.labs.intentslab D/AndroidRuntime﹕ Shutting  down VM
09-11 11:06:31.988    6070-6070/course.labs.intentslab W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb181c678)
09-11 11:06:31.988    6070-6070/course.labs.intentslab E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.app.SuperNotCalledException: Activity {android/com.android.internal.app.ChooserActivity} did not call through to super.onCreate()
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2177)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
        at android.app.ActivityThread.access0(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

Java函数:

private void startImplicitActivation() {

    Intent baseIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(URL));

    Intent chooserIntent = new Intent();
    chooserIntent.createChooser(baseIntent, "Open Browser...");

    startActivity(chooserIntent);
}

ChooserActivity.java(这是我在 SDK 路径中找到的 ChooserActivity.java。我不知道如何在 Android Studio 中找到该文件。

package com.android.internal.app;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;

public class ChooserActivity extends ResolverActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Intent intent = getIntent();
        Parcelable targetParcelable = intent.getParcelableExtra(Intent.EXTRA_INTENT);
        if (!(targetParcelable instanceof Intent)) {
            Log.w("ChooseActivity", "Target is not an intent: " +   targetParcelable);
            finish();
            return;
        }
        Intent target = (Intent)targetParcelable;
        CharSequence title = intent.getCharSequenceExtra(Intent.EXTRA_TITLE);
        if (title == null) {
            title = getResources().getText(com.android.internal.R.string.chooseActivity);
        }
        Parcelable[] pa = intent.getParcelableArrayExtra(Intent.EXTRA_INITIAL_INTENTS);
        Intent[] initialIntents = null;
        if (pa != null) {
            initialIntents = new Intent[pa.length];
            for (int i=0; i<pa.length; i++) {
                if (!(pa[i] instanceof Intent)) {
                    Log.w("ChooseActivity", "Initial intent #" + i
                            + " not an Intent: " + pa[i]);
                    finish();
                    return;
                }
                initialIntents[i] = (Intent)pa[i];
            }
        }
        super.onCreate(savedInstanceState, target, title, initialIntents, null, false);
    }
}

更新:再次尝试后,它显示了一条不同的错误消息。也许这有帮助。

09-11 13:07:00.900  21068-21068/course.labs.intentslab D/﹕  HostConnection::get() New Host Connection established 0xb8064840, tid 21068
09-11 13:07:00.970  21068-21068/course.labs.intentslab W/EGL_emulation﹕ eglSurfaceAttrib not implemented
09-11 13:07:00.970  21068-21068/course.labs.intentslab D/OpenGLRenderer﹕ Enabling debug mode 0
09-11 13:07:03.120  21068-21068/course.labs.intentslab I/Lab-Intents﹕ Entered startImplicitActivation()
09-11 13:07:03.120  21068-21068/course.labs.intentslab I/Lab-Intents﹕ Chooser Intent Action:null
09-11 13:07:03.120  21068-21068/course.labs.intentslab D/AndroidRuntime﹕ Shutting down VM
09-11 13:07:03.120  21068-21068/course.labs.intentslab W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb17b5678)
09-11 13:07:03.120  21068-21068/course.labs.intentslab E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.content.ActivityNotFoundException: No Activity found to handle Intent {  }

首先Intent.createChooser(Intent, CharSequence)是静态方法。启动意图就像

一样简单
private void startImplicitActivation() {
    Intent baseIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(URL));
    startActivity(Intent.createChooser(baseIntent, "Open Browser..."));
}

这就是您更新错误消息的原因。您最有可能通过使用 new Intent(Intent.ACTION_CHOOSER) 创建选择器获得的第一个。