我试图按照tutorial创建导航抽屉。当我建立并运行时,它刚刚崩溃 这是我的logcat:
at com.infintro.keondrae.everythingminecraft.FragmentNavigationDrawer.setupDrawerConfiguration(FragmentNavigationDrawer.java:63)
at com.infintro.keondrae.everythingminecraft.MainActivity.onCreate(MainActivity.java:22)
从logcat我得知MainActivity的第22行和FragmentNavigationDrawer的第63行有一个错误。 MainActivity的第22行是布局的onCreate。
MainActivity
public class MainActivity extends FragmentActivity {
private FragmentNavigationDrawer dlDrawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find our drawer view
dlDrawer = (FragmentNavigationDrawer) findViewById(R.id.drawer_layout);
// Setup drawer view
**Line 22** dlDrawer.setupDrawerConfiguration((ListView) findViewById(R.id.lvDrawer),
R.layout.drawer_nav_item, R.id.flContent);
// Add nav items
dlDrawer.addNavItem("Welcome", "Welcome Fragment", Welcome.class);
dlDrawer.addNavItem("Blueprints", "Blueprints Fragment", Blueprints.class);
dlDrawer.addNavItem("Chat", "Chat Fragment", Chat.class);
dlDrawer.addNavItem("Redstone", "Redstone Fragment", Redstone.class);
dlDrawer.addNavItem("Tutorials", "Tutorials Fragment", Tutorials.class);
dlDrawer.addNavItem("Gameplay", "Gameplay Fragment", Gameplay.class);
dlDrawer.addNavItem("Maps/Mods", "Maps/Mods Fragment", MapsMods.class);
dlDrawer.addNavItem("Wallpapers", "Wallpapers Fragment", Wallpapers.class);
dlDrawer.addNavItem("About", "About Fragment", About.class);
// Select default
if (savedInstanceState == null) {
dlDrawer.selectDrawerItem(0);
}
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content
if (dlDrawer.isDrawerOpen()) {
// Uncomment to hide menu items
// menu.findItem(R.id.mi_test).setVisible(false);
}
return super.onPrepareOptionsMenu(menu);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// Uncomment to inflate menu items to Action Bar
// inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@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 (dlDrawer.getDrawerToggle().onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
dlDrawer.getDrawerToggle().syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
dlDrawer.getDrawerToggle().onConfigurationChanged(newConfig);
}
FragmentNavigationDrawer.java 这是其中的一部分。第63行是getActionBar()。setDisplayHomeAsUpEnabled(true);
// setupDrawerConfiguration((ListView) findViewById(R.id.lvDrawer), R.layout.drawer_list_item, R.id.flContent);
public void setupDrawerConfiguration(ListView drawerListView, int drawerItemRes, int drawerContainerRes) {
// Setup navigation items array
drawerNavItems = new ArrayList<FragmentNavigationDrawer.FragmentNavItem>();
// Set the adapter for the list view
drawerAdapter = new ArrayAdapter<String>(getActivity(), drawerItemRes, new ArrayList<String>());
this.drawerContainerRes = drawerContainerRes;
// Setup drawer list view and related adapter
lvDrawer = drawerListView;
lvDrawer.setAdapter(drawerAdapter);
// Setup item listener
lvDrawer.setOnItemClickListener(new FragmentDrawerItemListener());
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
drawerToggle = setupDrawerToggle();
setDrawerListener(drawerToggle);
// set a custom shadow that overlays the main content when the drawer
setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// Setup action buttons
getActionBar().setDisplayHomeAsUpEnabled(true);
**Line 63** getActionBar().setHomeButtonEnabled(true);
}
布局:
activity_main
<com.infintro.keondrae.everythingminecraft.FragmentNavigationDrawer
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/lvDrawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/darker_gray"
android:dividerHeight="0dp"
android:background="@android:color/background_light"
/>
drawer_nav_item
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/list_selector_background"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textSize="16sp"
android:textColor="#111" />
任何人都可以帮助我吗?