我的应用程序工作正常,但是当我尝试在我的主类左侧添加侧边菜单时,我在第75行得到错误,我不明白,请帮助我。
line 75: mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
MainDrawer.java
package com.android.moyenne.activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuItem.OnMenuItemClickListener;
import com.android.activity.R;
import com.android.moyenne.ansco.ListAnSco;
public class MainDrawer extends SherlockFragmentActivity {
// Declare Variables
DrawerLayout mDrawerLayout;
ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;
MenuListAdapter mMenuAdapter;
String[] title;
String[] subtitle;
int[] icon;
Fragment aide = new Aide();
Fragment about = new About();
Fragment moyenne = new MoyenneMain();
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private static String aideTitle, aboutTitle,studentTitle;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from drawer_main.xml
setContentView(R.layout.drawer_main);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
BitmapDrawable bg = (BitmapDrawable)getResources().getDrawable(R.drawable.bg_striped);
bg.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
getSupportActionBar().setBackgroundDrawable(bg);
BitmapDrawable bgSplit = (BitmapDrawable)getResources().getDrawable(R.drawable.bg_striped_split_img);
bgSplit.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
getSupportActionBar().setSplitBackgroundDrawable(bgSplit);
}
// Get the Title
mTitle = mDrawerTitle = getTitle();
// Generate title
aideTitle = getResources().getString(R.string.aide);
aboutTitle = getResources().getString(R.string.about);
studentTitle = getResources().getString(R.string.app_title);
title = new String[] { studentTitle, aideTitle, aboutTitle };
// Generate subtitle
subtitle = new String[] { "Menu principal", "Aide sur l'application",
"A propos de l'application" };
// Generate icon
icon = new int[] { R.drawable.home, android.R.drawable.ic_menu_help, android.R.drawable.ic_menu_info_details};
// Locate DrawerLayout in drawer_main.xml
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
// Locate ListView in drawer_main.xml
mDrawerList = (ListView) findViewById(R.id.listview_drawer);
// Set a custom shadow that overlays the main content when the drawer
// opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
// Pass string arrays to MenuListAdapter
mMenuAdapter = new MenuListAdapter(MainDrawer.this, title, subtitle,
icon);
// Set the MenuListAdapter to the ListView
mDrawerList.setAdapter(mMenuAdapter);
// Capture listview menu item click
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// Enable ActionBar app icon to behave as action to toggle nav drawer
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
@Override
public void onDrawerClosed(View view) {
// TODO Auto-generated method stub
super.onDrawerClosed(view);
}
@Override
public void onDrawerOpened(View drawerView) {
// TODO Auto-generated method stub
// Set the title on the action when drawer open
getSupportActionBar().setTitle(mDrawerTitle);
super.onDrawerOpened(drawerView);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
@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);
}
// ListView click listener in the navigation drawer
private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
selectItem(position);
}
}
private void selectItem(int position) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Locate Position
switch (position) {
case 0:
ft.replace(R.id.content_frame, moyenne);
break;
case 1:
ft.replace(R.id.content_frame, aide);
break;
case 2:
ft.replace(R.id.content_frame, about);
break;
}
ft.commit();
mDrawerList.setItemChecked(position, true);
// Get the title followed by the position
setTitle(title[position]);
// Close drawer
mDrawerLayout.closeDrawer(mDrawerList);
title = new String[] { studentTitle, aideTitle, aboutTitle};
// Generate subtitle
subtitle = new String[] { "Menu principal", "Aide sur l'application",
"A propos de l'application", "Permet de prendre des notes" };
}
@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 toggles
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Get the options menu view from menu.xml in menu folder
getSupportMenuInflater().inflate(R.menu.menu, menu);
// Show the settings menu item in menu.xml
MenuItem menuSettings = menu.findItem(R.id.menu_settings);
// Capture menu item clicks
menuSettings.setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// TODO Auto-generated method stub
// Do something here
Intent intent = new Intent(getApplicationContext(),
ListAnSco.class);
startActivity(intent);
finish();
return false;
}
});
return true;
}
// Capture first menu button click
OnMenuItemClickListener setButtonClickListener = new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
// Create a simple toast message
Toast.makeText(MainDrawer.this, "Set Button", Toast.LENGTH_SHORT)
.show();
// Do something else
return false;
}
};
}
drawer_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/listview_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@drawable/bg_striped"
android:choiceMode="singleChoice"
android:dividerHeight="2dip"
android:divider="@drawable/list_divider_bl" />
</android.support.v4.widget.DrawerLayout>
日志:
10-29 00:28:31.773: E/AndroidRuntime(1241): FATAL EXCEPTION: main
10-29 00:28:31.773: E/AndroidRuntime(1241): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.activity/com.android.moyenne.activity.MainDrawer}: java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.support.v4.widget.DrawerLayout
10-29 00:28:31.773: E/AndroidRuntime(1241): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-29 00:28:31.773: E/AndroidRuntime(1241): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-29 00:28:31.773: E/AndroidRuntime(1241): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-29 00:28:31.773: E/AndroidRuntime(1241): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-29 00:28:31.773: E/AndroidRuntime(1241): at android.os.Handler.dispatchMessage(Handler.java:99)
10-29 00:28:31.773: E/AndroidRuntime(1241): at android.os.Looper.loop(Looper.java:137)
10-29 00:28:31.773: E/AndroidRuntime(1241): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-29 00:28:31.773: E/AndroidRuntime(1241): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 00:28:31.773: E/AndroidRuntime(1241): at java.lang.reflect.Method.invoke(Method.java:525)
10-29 00:28:31.773: E/AndroidRuntime(1241): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-29 00:28:31.773: E/AndroidRuntime(1241): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-29 00:28:31.773: E/AndroidRuntime(1241): at dalvik.system.NativeStart.main(Native Method)
10-29 00:28:31.773: E/AndroidRuntime(1241): Caused by: java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.support.v4.widget.DrawerLayout
10-29 00:28:31.773: E/AndroidRuntime(1241): at com.android.moyenne.activity.MainDrawer.onCreate(MainDrawer.java:75)
10-29 00:28:31.773: E/AndroidRuntime(1241): at android.app.Activity.performCreate(Activity.java:5133)
10-29 00:28:31.773: E/AndroidRuntime(1241): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-29 00:28:31.773: E/AndroidRuntime(1241): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-29 00:28:31.773: E/AndroidRuntime(1241): ... 11 more
答案 0 :(得分:1)
我无法在您的代码中找到任何问题。只需尝试Project--> Clean
并在uninstalling Previous Build
之后再次运行它可能会解决您的问题。
答案 1 :(得分:0)
尝试捕获异常。
public boolean onPrepareOptionsMenu(Menu menu) {
try {
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_Layout);
boolean drawerOpen = mDrawerLayout.isDrawerOpen(GravityCompat.START);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
}
catch (Exception e) {
}
return super.onPrepareOptionsMenu(menu);
}