尝试在空对象引用上调用虚方法'void android.app.ActionBar'

时间:2018-02-08 06:58:35

标签: android android-navigation-drawer

我有一个NavigationActivity和MainActivity,在NavigationActivity中我使用导航抽屉和MainActivity在MainActivity中扩展NavigationActivity我正在使用 Theme.AppCompat.Light.NoActionBar 并且还使用工具栏但我是获取空指针异常getsupportActionbar() 我已经尝试了以下代码来做到这一点

import android.content.Intent;
import android.content.res.Configuration;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import com.toolbarimport.AboutActivity;
import com.toolbarimport.ExchangeActivity;
import com.toolbarimport.R;     

public class NavigationActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
      private FrameLayout mainView;
      private NavigationView navigation_view;
      private DrawerLayout mDrawerLayout;
      private ActionBarDrawerToggle mDrawerToggle;


      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.activity_navigation);
        mainView = (FrameLayout) findViewById(R.id.view_stub);
        navigation_view = (NavigationView) findViewById(R.id.navigation_view);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 0, 0);
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        navigation_view.setNavigationItemSelectedListener(this);
        navigation_view.setItemIconTintList(null);
        navigation_view.setCheckedItem(R.id.home);
      }

      @Override
      protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
      }

      @Override
      public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        mDrawerToggle.onConfigurationChanged(newConfig);
      }

      @Override
      public void setContentView(int layoutResID) {
        if (mainView != null) {
          LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
          ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
                  ViewGroup.LayoutParams.MATCH_PARENT,
                  ViewGroup.LayoutParams.MATCH_PARENT);
          View stubView = inflater.inflate(layoutResID, mainView, false);
          mainView.addView(stubView, layoutParams);
        }
      }

      @Override
      public void setContentView(View view) {
        if (mainView != null) {
          ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
                  ViewGroup.LayoutParams.MATCH_PARENT,
                  ViewGroup.LayoutParams.MATCH_PARENT);
          mainView.addView(view, layoutParams);
        }
      }

      @Override
      public void setContentView(View view, ViewGroup.LayoutParams params) {
        if (mainView != null) {
          mainView.addView(view, params);
        }
      }

      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
          return true;
        }
        return super.onOptionsItemSelected(item);
      }


      @Override
      public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        return true;
      }
    }

MainActivity

public class MainActivity extends NavigationActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
        Toolbar toolbar=findViewById(R.id.my_toolbar);
        setSupportActionBar(toolbar);

    }


}

日志

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.toolbarimport/com.toolbarimport.Activity.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3003)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3064)
                                                                             at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1659)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:154)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:6816)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1565)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1453)
                                                                          Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
                                                                             at com.toolbarimport.Activity.NavigationActivity.onCreate(NavigationActivity.java:38)
                                                                             at com.toolbarimport.Activity.MainActivity.onCreate(MainActivity.java:12)
                                                                             at android.app.Activity.performCreate(Activity.java:6977)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2946)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3064) 
                                                                             at android.app.ActivityThread.-wrap14(ActivityThread.java) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1659) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:154) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:6816) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1565) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1453) 

0 个答案:

没有答案