ActionBarDrawerToggle上的NullPointerException

时间:2017-04-01 15:27:34

标签: java android nullpointerexception

我正在尝试创建一个自定义导航抽屉并发现此错误,所以我需要这个错误的意思,我怎么能解决这个错误,好像我再次遇到它 这是android监视器错误

04-01 17:20:57.017 25474-25474/abtech.waiteriano.com.dineinpos I/art: Late-enabling -Xcheck:jni
04-01 17:20:57.094 25474-25474/abtech.waiteriano.com.dineinpos D/ActivityThread: hoder:android.app.IActivityManager$ContentProviderHolder@2334fe75,provider,holder.Provider:android.content.ContentProviderProxy@26962e0a
04-01 17:20:57.104 25474-25474/abtech.waiteriano.com.dineinpos D/ActivityThread: BIND_APPLICATION handled : 0 / AppBindData{appInfo=ApplicationInfo{357afc98 abtech.waiteriano.com.dineinpos}}
04-01 17:20:57.105 25474-25474/abtech.waiteriano.com.dineinpos V/ActivityThread: Handling launch of ActivityRecord{33e40cf1 token=android.os.BinderProxy@77120d6 {abtech.waiteriano.com.dineinpos/abtech.waiteriano.com.dineinpos.MainActivity}}
04-01 17:20:57.114 25474-25474/abtech.waiteriano.com.dineinpos V/ActivityThread: ActivityRecord{33e40cf1 token=android.os.BinderProxy@77120d6 {abtech.waiteriano.com.dineinpos/abtech.waiteriano.com.dineinpos.MainActivity}}: app=android.app.Application@226f4a44, appName=abtech.waiteriano.com.dineinpos, pkg=abtech.waiteriano.com.dineinpos, comp={abtech.waiteriano.com.dineinpos/abtech.waiteriano.com.dineinpos.MainActivity}, dir=/data/app/abtech.waiteriano.com.dineinpos-1/base.apk
04-01 17:20:57.115 25474-25474/abtech.waiteriano.com.dineinpos D/FeatureProxyBase: FeatureProxyBase class constructor
04-01 17:20:57.115 25474-25474/abtech.waiteriano.com.dineinpos D/FeatureProxyBase: getService(), serviceName = multiwindow_service_v1
04-01 17:20:57.118 25474-25474/abtech.waiteriano.com.dineinpos D/FeatureProxyBase: FeatureProxyBase class constructor
04-01 17:20:57.118 25474-25474/abtech.waiteriano.com.dineinpos D/FeatureProxyBase: getService(), serviceName = multiwindow_service_v1
04-01 17:20:57.118 25474-25474/abtech.waiteriano.com.dineinpos D/FeatureProxyBase: FeatureProxyBase class constructor
04-01 17:20:57.118 25474-25474/abtech.waiteriano.com.dineinpos D/FeatureProxyBase: getService(), serviceName = multiwindow_service_v1
04-01 17:20:57.128 25474-25474/abtech.waiteriano.com.dineinpos W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
04-01 17:20:57.143 25474-25474/abtech.waiteriano.com.dineinpos D/AccessibilityManager: setStateLocked: wasEnabled = false, mIsEnabled = false, wasTouchExplorationEnabled = false, mIsTouchExplorationEnabled = false, wasHighTextContrastEnabled = false, mIsHighTextContrastEnabled = false
                                                                                       java.lang.Throwable: setStateLocked
                                                                                           at android.view.accessibility.AccessibilityManager.setStateLocked(AccessibilityManager.java:553)
                                                                                           at android.view.accessibility.AccessibilityManager.tryConnectToServiceLocked(AccessibilityManager.java:636)
                                                                                           at android.view.accessibility.AccessibilityManager.<init>(AccessibilityManager.java:226)
                                                                                           at android.view.accessibility.AccessibilityManager.getInstance(AccessibilityManager.java:206)
                                                                                           at android.view.View.setFlags(View.java:9922)
                                                                                           at android.view.ViewGroup.initViewGroup(ViewGroup.java:536)
                                                                                           at android.view.ViewGroup.<init>(ViewGroup.java:525)
                                                                                           at android.view.ViewGroup.<init>(ViewGroup.java:520)
                                                                                           at android.view.ViewGroup.<init>(ViewGroup.java:516)
                                                                                           at android.view.ViewGroup.<init>(ViewGroup.java:512)
                                                                                           at android.widget.FrameLayout.<init>(FrameLayout.java:119)
                                                                                           at com.android.internal.policy.impl.PhoneWindow$DecorView.<init>(PhoneWindow.java:2359)
                                                                                           at com.android.internal.policy.impl.PhoneWindow.generateDecor(PhoneWindow.java:3527)
                                                                                           at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3939)
                                                                                           at com.android.internal.policy.impl.PhoneWindow.getDecorView(PhoneWindow.java:2075)
                                                                                           at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:375)
                                                                                           at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:324)
                                                                                           at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:285)
                                                                                           at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                                           at abtech.waiteriano.com.dineinpos.MainActivity.onCreate(MainActivity.java:37)
                                                                                           at android.app.Activity.performCreate(Activity.java:6115)
                                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
                                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2573)
                                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2706)
                                                                                           at android.app.ActivityThread.access$800(ActivityThread.java:178)
                                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1544)
                                                                                           at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                                           at android.os.Looper.loop(Looper.java:214)
                                                                                           at android.app.ActivityThread.main(ActivityThread.java:6102)
                                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                                           at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
                                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
04-01 17:20:57.204 25474-25474/abtech.waiteriano.com.dineinpos D/AbsListView: checkAbsListViewlLogProperty get invalid command
04-01 17:20:57.208 25474-25474/abtech.waiteriano.com.dineinpos E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: abtech.waiteriano.com.dineinpos, PID: 25474
                                                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{abtech.waiteriano.com.dineinpos/abtech.waiteriano.com.dineinpos.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.ActionBarDrawerToggle.syncState()' on a null object reference
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2626)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2706)
                                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:178)
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1544)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                                     at android.os.Looper.loop(Looper.java:214)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:6102)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
                                                                                  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.ActionBarDrawerToggle.syncState()' on a null object reference
                                                                                     at abtech.waiteriano.com.dineinpos.MainActivity.onPostCreate(MainActivity.java:124)
                                                                                     at android.app.Instrumentation.callActivityOnPostCreate(Instrumentation.java:1229)
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2602)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2706) 
                                                                                     at android.app.ActivityThread.access$800(ActivityThread.java:178) 
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1544) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                                     at android.os.Looper.loop(Looper.java:214) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:6102) 
                                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                                     at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028) 
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 
04-01 17:24:27.269 25474-25474/abtech.waiteriano.com.dineinpos D/Process: killProcess, pid=25474
04-01 17:24:27.270 25474-25474/abtech.waiteriano.com.dineinpos D/Process: com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException:138 java.lang.ThreadGroup.uncaughtException:693 java.lang.ThreadGroup.uncaughtException:690 
04-01 17:24:27.270 25474-25474/abtech.waiteriano.com.dineinpos W/HTCLOG: use specified tag [Process], func [0].
04-01 17:24:27.270 25474-25474/abtech.waiteriano.com.dineinpos W/HTCLOG: mask=0x18

............................................... ......................... 这是我的活动,其中包含错误

package abtech.waiteriano.com.dineinpos;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.List;

import abtech.waiteriano.com.dineinpos.adapters.CustomDrawerAdapter;
import abtech.waiteriano.com.dineinpos.fragments.FragmentOne;
import abtech.waiteriano.com.dineinpos.fragments.FragmentThree;
import abtech.waiteriano.com.dineinpos.fragments.FragmentTwo;
import abtech.waiteriano.com.dineinpos.gettersandsetter.DrawerItem;

public class MainActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    CustomDrawerAdapter adapter;

    List<DrawerItem> dataList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Initializing
        dataList = new ArrayList<DrawerItem>();
        mTitle = mDrawerTitle = getTitle();
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

//        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
//                GravityCompat.START);

        // Add Drawer Item to dataList
        // Add Drawer Item to dataList
        dataList.add(new DrawerItem("Message", R.mipmap.ic_launcher));
        dataList.add(new DrawerItem("Likes", R.mipmap.ic_launcher));
        dataList.add(new DrawerItem("Games", R.mipmap.ic_launcher));
        dataList.add(new DrawerItem("Lables", R.mipmap.ic_launcher));
        dataList.add(new DrawerItem("Search", R.mipmap.ic_launcher));
        dataList.add(new DrawerItem("Cloud", R.mipmap.ic_launcher));
        dataList.add(new DrawerItem("Camara", R.mipmap.ic_launcher));
        dataList.add(new DrawerItem("Video", R.mipmap.ic_launcher));
        dataList.add(new DrawerItem("Groups", R.mipmap.ic_launcher));
        dataList.add(new DrawerItem("Import & Export",
                R.mipmap.ic_launcher));
        dataList.add(new DrawerItem("About", R.mipmap.ic_launcher));
        dataList.add(new DrawerItem("Settings", R.mipmap.ic_launcher));
        dataList.add(new DrawerItem("Help", R.mipmap.ic_launcher));

        adapter = new CustomDrawerAdapter(this, R.layout.custom_drawer_item,
                dataList);

        mDrawerList.setAdapter(adapter);
    }
    public void SelectItem(int possition) {

        Fragment fragment = null;
        Bundle args = new Bundle();
        switch (possition) {
            case 0:
                fragment = new FragmentOne();
                args.putString(FragmentOne.ITEM_NAME, dataList.get(possition)
                        .getItemName());
                args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition)
                        .getImgResID());
                break;
            case 1:
                fragment = new FragmentTwo();
                args.putString(FragmentTwo.ITEM_NAME, dataList.get(possition)
                        .getItemName());
                args.putInt(FragmentTwo.IMAGE_RESOURCE_ID, dataList.get(possition)
                        .getImgResID());
                break;
            case 2:
                fragment = new FragmentThree();
                args.putString(FragmentThree.ITEM_NAME, dataList.get(possition)
                        .getItemName());
                args.putInt(FragmentThree.IMAGE_RESOURCE_ID, dataList.get(possition)
                        .getImgResID());
                break;
            case 3:
                fragment = new FragmentOne();
                args.putString(FragmentOne.ITEM_NAME, dataList.get(possition)
                        .getItemName());
                args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition)
                        .getImgResID());
                break;
        }

        fragment.setArguments(args);
        FragmentManager frgManager = getFragmentManager();
        frgManager.beginTransaction().replace(R.id.content_frame, fragment)
                .commit();

        mDrawerList.setItemChecked(possition, true);
        setTitle(dataList.get(possition).getItemName());
        mDrawerLayout.closeDrawer(mDrawerList);

    }
    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActionBar().setTitle(mTitle);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // The action bar home/up action should open or close the drawer.
        // ActionBarDrawerToggle will take care of this.
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        return false;
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggles
        mDrawerToggle.onConfigurationChanged(newConfig);
    }
}

1 个答案:

答案 0 :(得分:1)

你永远不会初始化private ActionBarDrawerToggle mDrawerToggle; 没有引用任何定义的DrawerToggle,因此它是null。这导致了这个错误。通过按id查找视图或在本地初始化它来初始化。

快乐编码:)

在任何访问之前,在onCreate中调用此子例程。

  void setupDrawerToggle(){
    mDrawerToggle = new android.support.v7.app.ActionBarDrawerToggle(this,mDrawerLayout,toolbar,R.string.app_name, R.string.app_name);
    //This is necessary to change the icon of the Drawer Toggle upon state change.
    mDrawerToggle.syncState();
}