Android在意图中检查操作并执行一些逻辑

时间:2016-07-26 09:40:14

标签: android android-activity nullpointerexception intentfilter

我在一个活动中设置了两个intent过滤器。 `

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <data  android:scheme="https://dou.ua/lenta/" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter> `

然后检查尝试检查创建活动的操作。

 protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);
    ButterKnife.bind(this);
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);

    Intent intent = getIntent();
    String action = intent.getAction();
    if (action.equals("android.intent.action.VIEW ")) {

    } else {
      getSupportFragmentManager().addOnBackStackChangedListener(this);
      isDisplayingHomeBtn();
      getSupportFragmentManager().beginTransaction()
          .replace(R.id.container, new FeedFragment())
          .commit();
    }
  }

但我有一个NullPointerException

  

流程:com.bogdan_kolomiets_1996.bogdan.dou_feed,PID:5237                                                                                            java.lang.RuntimeException:无法启动活动   ComponentInfo {com.bogdan_kolomiets_1996.bogdan.dou_feed / com.bogdan_kolomiets_1996.bogdan.dou_feed.ui.MainActivity}:   java.lang.NullPointerException:尝试调用虚方法   'java.lang.Object android.os.Bundle.get(java.lang.String)'为null   对象参考                                                                                                在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2622)                                                                                                在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2692)                                                                                                在android.app.ActivityThread.access $ 800(ActivityThread.java:179)                                                                                                在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1540)                                                                                                在android.os.Handler.dispatchMessage(Handler.java:111)                                                                                                在android.os.Looper.loop(Looper.java:194)                                                                                                在android.app.ActivityThread.main(ActivityThread.java:5795)                                                                                                at java.lang.reflect.Method.invoke(Native Method)                                                                                                在java.lang.reflect.Method.invoke(Method.java:372)                                                                                                在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1005)                                                                                                在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:800)                                                                                                at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:114)                                                                                             引起:java.lang.NullPointerException:尝试调用虚拟   方法'java.lang.Object android.os.Bundle.get(java.lang.String)'上的一个   null对象引用                                                                                                在   com.bogdan_kolomiets_1996.bogdan.dou_feed.ui.MainActivity.onCreate(MainActivity.java:38)                                                                                                在android.app.Activity.performCreate(Activity.java:6071)                                                                                                在   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)                                                                                                在   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2575)                                                                                                在   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2692)                                                                                                在android.app.ActivityThread.access $ 800(ActivityThread.java:179)                                                                                                在   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1540)                                                                                                在android.os.Handler.dispatchMessage(Handler.java:111)                                                                                                在android.os.Looper.loop(Looper.java:194)                                                                                                在android.app.ActivityThread.main(ActivityThread.java:5795)                                                                                                at java.lang.reflect.Method.invoke(Native Method)                                                                                                在java.lang.reflect.Method.invoke(Method.java:372)

1 个答案:

答案 0 :(得分:0)

你确定,你的活动放在这里&com; bogdan_kolomiets_1996.bogdan.dou_feed / com.bogdan_kolomiets_1996.bogdan.dou_feed.ui.MainActivity&#39;?

当您尝试开始活动时,此错误就会出现,而不是在您尝试进行操作时。