当我尝试从空活动中打开导航抽屉活动时,为什么我的应用程序崩溃?

时间:2018-08-09 14:58:08

标签: java android android-intent navigation-drawer

我是android的初学者我的应用程序从启动屏幕启动,该屏幕进入一个空的活动,需要一些信息。到这里为止一切正常。之后,我使用了一个“提交”按钮来引导用户进行“导航”抽屉活动,但是该应用程序崩溃了。我试图用单击按钮时的Empty活动替换导航抽屉活动,效果很好。我还尝试从启动屏幕启动导航抽屉活动,但效果不佳。

submitButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent gotoChoiceSelection = new Intent(getApplicationContext(), ChoiceSelection.class);
            startActivity(gotoChoiceSelection);
        }
    });

(ChoiceSelection是我的导航抽屉活动)

logcat

08-09 20:39:15.765 19793-19793/comviewappisome.google.sites.iamengineermu E/AndroidRuntime: FATAL EXCEPTION: main
Process: comviewappisome.google.sites.iamengineermu, PID: 19793
java.lang.RuntimeException: Unable to start activity ComponentInfo{comviewappisome.google.sites.iamengineermu/comviewappisome.google.sites.iamengineermu.ChoiceSelection}: android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2904)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2986)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1671)
    at android.os.Handler.dispatchMessage(Handler.java:108)
    at android.os.Looper.loop(Looper.java:206)
    at android.app.ActivityThread.main(ActivityThread.java:6784)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:852)
 Caused by: android.view.InflateException: Binary XML file line #18: Binary XML file line #18: Error inflating class fragment
 Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.app.ActionBar.getThemedContext()' on a null object reference
    at comviewappisome.google.sites.iamengineermu.NavigationDrawerFragment.onCreateView(NavigationDrawerFragment.java:101)
    at android.app.Fragment.performCreateView(Fragment.java:2611)
    at android.app.FragmentManagerImpl.ensureInflatedFragmentView(FragmentManager.java:1459)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1247)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1454)
    at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1701)
    at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3611)
    at android.app.FragmentController.onCreateView(FragmentController.java:98)
    at android.app.Activity.onCreateView(Activity.java:6196)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:428)
    at android.app.Activity.setContentView(Activity.java:2659)
    at comviewappisome.google.sites.iamengineermu.ChoiceSelection.onCreate(ChoiceSelection.java:37)
    at android.app.Activity.performCreate(Activity.java:6984)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1235)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2857)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2986)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1671)
    at android.os.Handler.dispatchMessage(Handler.java:108)
    at android.os.Looper.loop(Looper.java:206)
    at android.app.ActivityThread.main(ActivityThread.java:6784)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:852)

建议我为此提供解决方案。

2 个答案:

答案 0 :(得分:0)

如果您使用活动导航抽屉活动

您可以在 getApplicationContext()

处尝试

我认为您应该像这样更改Java代码:

submitButton.setOnClickListener(new View.OnClickListener() {
@Override
  public void onClick(View v) {
  Intent gotoChoiceSelection = new Intent(this, ChoiceSelection.class);
            startActivity(gotoChoiceSelection );
 }
});

答案 1 :(得分:0)

尝试此代码,对我有用:

submitButton.setOnClickListener(new View.OnClickListener() {
   @Override
    public void onClick(View v) {
    Intent gotoChoiceSelection = new Intent(this, ChoiceSelection.class);
        startActivity(gotoChoiceSelection );
 }
});

只需将getApplicationContext()替换为this