Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:MainActivity中的NullPointerException扩展了BaseActivity_Android_Nullpointerexception_Navigation Drawer - Fatal编程技术网

Android:MainActivity中的NullPointerException扩展了BaseActivity

Android:MainActivity中的NullPointerException扩展了BaseActivity,android,nullpointerexception,navigation-drawer,Android,Nullpointerexception,Navigation Drawer,我试图在扩展FragmentActivity的类BaseActivity中创建一个导航抽屉、菜单和操作栏 但是在MainActivity类中,我在mDrawerList.setDivider(新的GradientDrawable(Orientation.RIGHT_LEFT,colors))一行上得到了一个错误。这是日志: 03-21 10:20:41.628: E/AndroidRuntime(9704): Caused by: java.lang.NullPointerException 0

我试图在扩展FragmentActivity的类BaseActivity中创建一个导航抽屉、菜单和操作栏

但是在MainActivity类中,我在
mDrawerList.setDivider(新的GradientDrawable(Orientation.RIGHT_LEFT,colors))一行上得到了一个错误。这是日志:

03-21 10:20:41.628: E/AndroidRuntime(9704): Caused by: java.lang.NullPointerException
03-21 10:20:41.628: E/AndroidRuntime(9704):     at com.example.BaseActivity.cargarDrawerLayout(BaseActivity.java:194)
03-21 10:20:41.628: E/AndroidRuntime(9704):     at com.example.BaseActivity.onCreate(BaseActivity.java:75)
03-21 10:20:41.628: E/AndroidRuntime(9704):     at com.example.MainActivity.onCreate(MainActivity.java:192)
03-21 10:20:41.628: E/AndroidRuntime(9704):     at android.app.Activity.performCreate(Activity.java:4465)
03-21 10:20:41.628: E/AndroidRuntime(9704):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-21 10:20:41.628: E/AndroidRuntime(9704):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2033)
可能是什么错误?谢谢

基本活动

public class BaseActivity extends FragmentActivity {

PerfilObj perfilObj;

SoundManager sound;
int sonidoMenu;

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

private CharSequence mDrawerTitle;
private CharSequence mTitle;

private String[] textosMenuLateral;
private TypedArray iconosMenuLateral1;
private TypedArray iconosMenuLateral2;

private ArrayList<DrawerItem> navDrawerItems1;
private ArrayList<DrawerItem> navDrawerItems2;
private DrawerListAdapter adapter;

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    PerfilAdapter.iniciarBaseDatos(this);
    perfilObj = PerfilAdapter.selectPerfil(1);

    requestWindowFeature(Window.FEATURE_ACTION_BAR);

    cargarActionBar();
    cargarDrawerLayout(savedInstanceState);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    switch (item.getItemId()) {

    case R.id.menuOpcSonidos:

        perfilObj = PerfilAdapter.selectPerfil(1);

        if (perfilObj.getSonidos().equals(Modulo.SONIDO_ON)) {
            perfilObj.setSonidos(Modulo.SONIDO_OFF);

            Modulo.toast(this, 
                    this, 
                    getResources().getString(R.string.sonidoOnOff) + " | <b>OFF</b>", 
                    perfilObj.getColor(), 
                    R.drawable.sonido_off, 
                    Toast.LENGTH_SHORT);

        } else {
            perfilObj.setSonidos(Modulo.SONIDO_ON);

            Modulo.toast(this, 
                    this, 
                    getResources().getString(R.string.sonidoOnOff) + " | <b>ON</b>", 
                    perfilObj.getColor(), 
                    R.drawable.sonido_on, 
                    Toast.LENGTH_SHORT);
        }
        PerfilAdapter.updatePerfil(perfilObj);
        return true;

    case R.id.menuOpcCambiarColor:

        CambiarColores colores = new CambiarColores(this, 
                R.layout.cambiar_colores, 
                "Color", 
                R.id.botonAplicarColor,
                0); 

        colores.show();

        return true;

    default:
        return super.onOptionsItemSelected(item);
    }
}

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    menu.findItem(R.id.menuOpcSonidos)
        .setTitle(getResources().getString(R.string.sonidoOnOff) + " " + perfilObj.getSonidos());

    return super.onPrepareOptionsMenu(menu);
}

private void cargarActionBar() {

    ActionBar actionBar = getActionBar();
    int[] colores2 = Modulo.cargarColoresDrawerlayout(perfilObj.getColor());
    actionBar.setBackgroundDrawable(new GradientDrawable(Orientation.BOTTOM_TOP, colores2));

    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    int titleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
    TextView textoTitulo = (TextView)findViewById(titleId);
    textoTitulo.setTextColor(getResources().getColor(R.color.blanco));
    textoTitulo.setTypeface(null, Typeface.BOLD);
    textoTitulo.setTextSize(19);
    textoTitulo.setShadowLayer(5, 0, 0, getResources().getColor(R.color.negro));        
}

private void cargarDrawerLayout(Bundle b) {

    mTitle = mDrawerTitle = getTitle();

    textosMenuLateral = getResources().getStringArray(R.array.nav_drawer_items);

    iconosMenuLateral1 = getResources()
            .obtainTypedArray(R.array.iconos_menu_lateral1);

    iconosMenuLateral2 = getResources()
            .obtainTypedArray(R.array.iconos_menu_lateral2);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.list_slidermenuMain);

    int[] colores = {0, 0xFFFFFFFF, 0};

             ERROR IN THIS LINE
    mDrawerList.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colores));
    mDrawerList.setDividerHeight(4);

    navDrawerItems1 = new ArrayList<DrawerItem>();

    navDrawerItems1.add(new DrawerItem(textosMenuLateral[0], iconosMenuLateral1.getResourceId(0, -1)));
    navDrawerItems1.add(new DrawerItem(textosMenuLateral[1], iconosMenuLateral1.getResourceId(1, -1)));
    navDrawerItems1.add(new DrawerItem(textosMenuLateral[2], iconosMenuLateral1.getResourceId(2, -1)));
    navDrawerItems1.add(new DrawerItem(textosMenuLateral[3], iconosMenuLateral1.getResourceId(3, -1)));
    navDrawerItems1.add(new DrawerItem(textosMenuLateral[4], iconosMenuLateral1.getResourceId(4, -1)));

    navDrawerItems2 = new ArrayList<DrawerItem>();

    navDrawerItems2.add(new DrawerItem(textosMenuLateral[0], iconosMenuLateral2.getResourceId(0, -1)));
    navDrawerItems2.add(new DrawerItem(textosMenuLateral[1], iconosMenuLateral2.getResourceId(1, -1)));
    navDrawerItems2.add(new DrawerItem(textosMenuLateral[2], iconosMenuLateral2.getResourceId(2, -1)));
    navDrawerItems2.add(new DrawerItem(textosMenuLateral[3], iconosMenuLateral2.getResourceId(3, -1)));
    navDrawerItems2.add(new DrawerItem(textosMenuLateral[4], iconosMenuLateral2.getResourceId(4, -1)));

    iconosMenuLateral1.recycle();
    iconosMenuLateral2.recycle();

    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

    int pos = 0;

    adapter = new DrawerListAdapter(getApplicationContext(),
            navDrawerItems1,
            navDrawerItems2,
            perfilObj.getColor(),
            pos);
    mDrawerList.setAdapter(adapter);

    mDrawerToggle = new ActionBarDrawerToggle(
            this, 
            mDrawerLayout,
            R.drawable.ic_drawer1, 
            R.string.app_name,
            R.string.app_name
    ) {
        public void onDrawerClosed(View view) {

            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu();
        } 

        public void onDrawerOpened(View drawerView) {

            getActionBar().setTitle(mDrawerTitle);

            invalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (b == null) {

        opcionesPanelLateral(0);
    }

}

private class SlideMenuClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        TextView textView = (TextView) view.findViewById(R.id.title);
        textView.setTypeface(null, Typeface.BOLD);    

        opcionesPanelLateral(position);
    }
}

private void opcionesPanelLateral(int position) {

    Intent i;

    switch (position) {
        case 0:
            break;
        case 1:
            i = new Intent(this, InfoOficinasTurismo.class);
            mDrawerLayout.closeDrawer(mDrawerList);
            startActivity(i);
            break;

        case 2:
            i = new Intent(this, BusTuristico.class);
            mDrawerLayout.closeDrawer(mDrawerList);
            startActivity(i);
            break;

        case 3:
            ElegirPlanos planos = new ElegirPlanos(this, 
                    R.layout.elegir_plano, 
                    R.string.planosMetroRenfe, 
                    R.id.botonPlanoMetro,
                    R.id.botonPlanoRenfe); 
            planos.show();
            break;

        case 4:

            break;

        default:
            break;
    }

    mDrawerList.setItemChecked(position, true);
    mDrawerList.setSelection(position);
    if (position == 0) {
        setTitle(getResources().getString(R.string.app_name));
    } else {
        setTitle(textosMenuLateral[position]);
    }

    mDrawerLayout.closeDrawer(mDrawerList);
}


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

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

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

public void onBackPressed() {

    mDrawerLayout.closeDrawer(mDrawerList);
}

}
 protected void onCreate(Bundle savedInstanceState) {

    iniciarBaseDatos();

    super.onCreate(savedInstanceState);

    sonidos();

    setContentView(R.layout.activity_main);

    declararObjetos();

    pintarLocalizacion(false);

    if (perfilObj.getTab().equals("2")) {
        tareaAsincMapa = new tareaAsincronaMapa();
        tareaAsincMapa.execute();
    }

    mapa.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    mapa.setMyLocationEnabled(true); 
    mapa.getUiSettings().setCompassEnabled(true);

    cargarTabHost();

    cambiarTipoMarkersMonumentos();

    cargarListview(savedInstanceState);
    cargarSpinner();

    cargarImagenes();

    pintarElementos();
}  
编辑

InfoOficinasTurismo

protected void onCreate(Bundle savedInstanceState) {

    iniciarBaseDatos();

    super.onCreate(savedInstanceState);
    setContentView(R.layout.oficinas_info);

    cargarExpandable();
}

您正在调用
super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)之前的
main活动中的code>。这意味着抽屉初始化发生在给定和查看活动之前-因此
NullPointerException


您可以在调用
super.onCreate(savedInstanceState)之前设置内容视图
或仅移动
BaseActivity.onPostCreate
中的初始化方法(我猜是cargarActionBar/cargarDrawerLayout)。我建议使用后一种方法,因为它更易于维护-只要您在子活动中的
onCreate
方法期间设置内容视图,它就会一直工作。

请显示布局XMLI已将方法置于onPostCreate中。抽屉的MainActivity和其他选项运行正常。但其他人在同一行失败。我已经编辑了我的代码。这个新类是从导航中调用的,它失败了,我的问题解决了。我忘了输入各种xml