我已经设置了意向过滤器来处理我的应用程序中的某些电子邮件链接。这在Android的基本邮件程序中运行良好。我点击链接,系统会提示我是使用浏览器还是我的应用程序打开它,选择我的应用后,它会得到正确处理。使用Gmail应用程序的情况并非如此。如何确保正确处理此应用程序?意向过滤器似乎有效——系统会提示我输入浏览器或应用程序,但在选择应用程序后,它会被迫关闭。这是我的日志中的错误:
06-02 01:23:11.337: ERROR/AndroidRuntime(11145): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android/com.example.android.Example}: java.lang.NullPointerException
06-02 01:23:11.337: ERROR/AndroidRuntime(11145): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-02 01:23:11.337: ERROR/AndroidRuntime(11145): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-02 01:23:11.337: ERROR/AndroidRuntime(11145): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-02 01:23:11.337: ERROR/AndroidRuntime(11145): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-02 01:23:11.337: ERROR/AndroidRuntime(11145): at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 01:23:11.337: ERROR/AndroidRuntime(11145): at android.os.Looper.loop(Looper.java:123)
06-02 01:23:11.337: ERROR/AndroidRuntime(11145): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-02 01:23:11.337: ERROR/AndroidRuntime(11145): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 01:23:11.337: ERROR/AndroidRuntime(11145): at java.lang.reflect.Method.invoke(Method.java:521)
06-02 01:23:11.337: ERROR/AndroidRuntime(11145): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
06-02 01:23:11.337: ERROR/AndroidRuntime(11145): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
06-02 01:23:11.337: ERROR/AndroidRuntime(11145): at dalvik.system.NativeStart.main(Native Method)
06-02 01:23:11.337: ERROR/AndroidRuntime(11145): Caused by: java.lang.NullPointerException
06-02 01:23:11.337: ERROR/AndroidRuntime(11145): at com.example.android.Example.onCreate(Example.java:301)
06-02 01:23:11.337: ERROR/AndroidRuntime(11145): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-02 01:23:11.337: ERROR/AndroidRuntime(11145): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-02 01:23:11.337: ERROR/AndroidRuntime(11145): ... 11 more
此外,这是我在onCreate:中处理传入意图的代码
if (getIntent().getCategories().contains("android.intent.category.BROWSABLE")){
try{
Uri uri = getIntent().getData();
String url = uri.getFragment();
//parse url and handle its contents
}catch(Exception e){
Toast.makeText(this, "could not open url", Toast.LENGTH_LONG).show();
}
}
任何帮助都将不胜感激!
getIntent()。如果没有类别,getCategories()将返回null。为了解决这个问题,我将if语句更改为:
if (getIntent().getAction().equals("android.intent.action.VIEW")){...}