我试图在扩展FragmentActivity的类BaseActivity中创建导航抽屉,菜单和ActionBar。
但是在MainActivity类中,我在行mDrawerList.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colores));
上收到错误。这是日志:
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)
错误是什么?感谢
BASEACTIVITY
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);
}
}
MAINACTIVITY
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();
}
答案 0 :(得分:0)
您在super.onCreate(savedInstanceState);
之前MainActivity
呼叫setContentView(R.layout.activity_main);
。这意味着抽屉初始化在给定活动和查看之前发生 - 因此NullPointerException
。
您可以在调用super.onCreate(savedInstanceState);
之前设置内容视图,也可以在BaseActivity.onPostCreate
中移动初始化方法(cargarActionBar / cargarDrawerLayout)。我建议使用后一种方法,因为它更容易维护 - 只要您在子活动的onCreate
方法中设置内容视图,它就会一直有效。