我想为每个新活动创建基本活动,有一个片段,可以创建5个新意图。 FourOne是第一个意图,这个是完美的,但其他人得到了nullpointerexception。我该如何解决这个问题?
BaseActivity
public class BaseActivity extends ActionBarActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
protected RelativeLayout _completeLayout, _activityLayout;
// nav drawer title
private CharSequence mDrawerTitle;
// used to store app title
private CharSequence mTitle;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.base_activity);
//if (savedInstanceState == null) {
//on first time display view for first nav item
//displayView(0);
// }
}
public void set(String[] menutitles,TypedArray menuIcons) {
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.slider_list);
navDrawerItems = new ArrayList<NavDrawerItem>();
// adding nav drawer items
if(menuIcons==null){
for(int i=0;i<menutitles.length;i++){
navDrawerItems.add(new NavDrawerItem(menutitles[i]));
}}else{
for(int i=0;i<menutitles.length;i++){
navDrawerItems.add(new NavDrawerItem(menutitles[i],menuIcons.getResourceId(i, -1)));
}
}
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
// enabling action bar app icon and behaving it as toggle button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
// getSupportActionBar().setIcon(R.drawable.ic_drawer);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, // nav menu toggle icon
R.string.app_name, // nav drawer open - description for
// accessibility
R.string.app_name // nav drawer close - description for
// accessibility
) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
supportInvalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
//getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
mDrawerLayout.closeDrawer(mDrawerList);
} else {
mDrawerLayout.openDrawer(mDrawerList);
}
}
return super.onOptionsItemSelected(item);
}
/***
* Called when invalidateOptionsMenu() is triggered
*/
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// if nav drawer is opened, hide the action items
// boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
// menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
switch (position) {
case 0:
Intent intent = new Intent(this, FourOne.class);
startActivity(intent);
finish();
break;
case 1:
Intent intent1 = new Intent(this, FourTwo.class);
startActivity(intent1);
finish();
break;
case 2:
Intent intent2 = new Intent(this, FourThree.class);
startActivity(intent2);
finish();
break;
case 3:
Intent intent3 = new Intent(this, FourFour.class);
startActivity(intent3);
finish();
break;
case 4:
Intent intent4 = new Intent(this, FourFive.class);
startActivity(intent4);
finish();
break;
default:
break;
}
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
/**
* When using the ActionBarDrawerToggle, you must call it during
* onPostCreate() and onConfigurationChanged()...
*/
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
第一个活动,这个活动
public class FourOne extends BaseActivity {
private String[] menutitles;
private TypedArray menuIcons;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.four_one);
menutitles = getResources().getStringArray(R.array.titlestwo); // load titles from strings.xml
menuIcons = getResources()
.obtainTypedArray(R.array.iconstwo);//load icons from strings.xml
set(menutitles, menuIcons);
}
}
第二项活动
public class FourTwo extends BaseActivity {
private String[] menutitles;
private TypedArray menuIcons;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.four_two);
menutitles = getResources().getStringArray(R.array.titlestwo); // load titles from strings.xml
menuIcons = getResources()
.obtainTypedArray(R.array.iconstwo);//load icons from strings.xml
set(menutitles, menuIcons);
}
}
logcat错误
07-15 07:36:17.647: E/AndroidRuntime(1703): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sggsoftware.airsoftg/com.sggsoftware.airsoftg.FourTwo}: java.lang.NullPointerException
07-15 07:36:17.647: E/AndroidRuntime(1703): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-15 07:36:17.647: E/AndroidRuntime(1703): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-15 07:36:17.647: E/AndroidRuntime(1703): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-15 07:36:17.647: E/AndroidRuntime(1703): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-15 07:36:17.647: E/AndroidRuntime(1703): at android.os.Handler.dispatchMessage(Handler.java:102)
07-15 07:36:17.647: E/AndroidRuntime(1703): at android.os.Looper.loop(Looper.java:136)
07-15 07:36:17.647: E/AndroidRuntime(1703): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-15 07:36:17.647: E/AndroidRuntime(1703): at java.lang.reflect.Method.invokeNative(Native Method)
07-15 07:36:17.647: E/AndroidRuntime(1703): at java.lang.reflect.Method.invoke(Method.java:515)
07-15 07:36:17.647: E/AndroidRuntime(1703): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-15 07:36:17.647: E/AndroidRuntime(1703): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-15 07:36:17.647: E/AndroidRuntime(1703): at dalvik.system.NativeStart.main(Native Method)
07-15 07:36:17.647: E/AndroidRuntime(1703): Caused by: java.lang.NullPointerException
07-15 07:36:17.647: E/AndroidRuntime(1703): at com.sggsoftware.airsoftg.BaseActivity.set(BaseActivity.java:65)
07-15 07:36:17.647: E/AndroidRuntime(1703): at com.sggsoftware.airsoftg.FourTwo.onCreate(FourTwo.java:28)
07-15 07:36:17.647: E/AndroidRuntime(1703): at android.app.Activity.performCreate(Activity.java:5231)
07-15 07:36:17.647: E/AndroidRuntime(1703): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-15 07:36:17.647: E/AndroidRuntime(1703): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
07-15 07:36:17.647: E/AndroidRuntime(1703): ... 11 more
答案 0 :(得分:0)
在扩展BaseActivity的某个Activity中调用onCreate时,您调用的是super.onCreate,即BaseActivity的onCreate。现在的问题是你在BaseActivity的onCreate中调用setContentView,然后再在实际的Activity里面调用。这意味着您的base_activity.xml分别由four_one.xml,four_two.xml,...替换。这意味着,除非像R.id.drawer_layout这样的视图也存在于four_one.xml中,依此类推,否则您将无法访问这些视图。因此NullPointerException。
有两种解决方案:
删除活动扩展,改为生成FourOne,FourTwo扩展片段,在base_activity.xml中放置容器布局,以及使用片段还需要做些什么。
从BaseActivity中删除onCreate,而是将所需的base_activity.xml中的所有视图分别放在不同的布局four_one.xml,four_two.xml,....使用include-tag,因此您不必重复XML代码。