public class HomeActivity extends Activity {
private String[] drawer_options;
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mTitle = mDrawerTitle = getTitle();
drawer_options = getResources().getStringArray(R.array.drawer_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, drawer_options));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// enable ActionBar app icon to behave as action to toggle nav drawer
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the sliding drawer and the action bar app icon
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
/* Called whenever we call invalidateOptionsMenu() */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
menu.findItem(R.id.ActionItem1).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(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 (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action buttons
switch(item.getItemId()) {
case R.id.ActionItem1:
// create intent to perform web search for this planet
Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, getActionBar().getTitle());
// catch event that there's no activity to handle intent
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Toast.makeText(this, R.string.app_not_available, Toast.LENGTH_LONG).show();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
/* The click listner for ListView 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) {
// update the main content by replacing fragments
Fragment fragment = new PlanetFragment();
Bundle args = new Bundle();
args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(drawer_options[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
@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);
}
/**
* Fragment that appears in the "content_frame"
*/
public static class PlanetFragment extends Fragment {
public static final String ARG_PLANET_NUMBER = "planet_number";
public PlanetFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.cliq_fragment, container, false);
int i = getArguments().getInt(ARG_PLANET_NUMBER);
String planet = getResources().getStringArray(R.array.drawer_array)[i];
int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
"drawable", getActivity().getPackageName());
((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
getActivity().setTitle(planet);
return rootView;
}}}
我正在尝试使用意图从另一个活动开始此活动。我不确定出了什么问题,或者我是否遗漏了某些东西,因为它在 onCreate() 处强制关闭我试图从 logcat 中查找错误,但我没有任何运气。这是开始此活动时的 logcat。
05-20 22:59:27.697: D/memalloc(21948): /dev/pmem: Unmapping buffer base:0x51d88000 size:4976640 offset:4853760
05-20 22:59:27.697: D/memalloc(21948): /dev/pmem: Unmapping buffer base:0x52247000 size:5099520 offset:4976640
05-20 22:59:27.697: D/memalloc(21948): /dev/pmem: Unmapping buffer base:0x52724000 size:5222400 offset:5099520
05-20 22:59:27.727: W/dalvikvm(21948): VFY: unable to resolve virtual method 53: Landroid/support/v4/widget/DrawerLayout;.closeDrawer (Landroid/view/View;)V
05-20 22:59:27.727: W/dalvikvm(21948): VFY: unable to resolve virtual method 50: Landroid/support/v4/app/ActionBarDrawerToggle;.onConfigurationChanged (Landroid/content/res/Configuration;)V
05-20 22:59:27.727: E/dalvikvm(21948): Could not find class 'android.support.v4.widget.DrawerLayout', referenced from method com.example.facecliq.HomeActivity.onCreate
05-20 22:59:27.727: W/dalvikvm(21948): VFY: unable to resolve check-cast 32 (Landroid/support/v4/widget/DrawerLayout;) in Lcom/example/facecliq/HomeActivity;
05-20 22:59:27.727: W/dalvikvm(21948): VFY: unable to resolve virtual method 51: Landroid/support/v4/app/ActionBarDrawerToggle;.onOptionsItemSelected (Landroid/view/MenuItem;)Z
05-20 22:59:27.727: W/dalvikvm(21948): VFY: unable to resolve virtual method 52: Landroid/support/v4/app/ActionBarDrawerToggle;.syncState ()V
05-20 22:59:27.727: W/dalvikvm(21948): VFY: unable to resolve virtual method 54: Landroid/support/v4/widget/DrawerLayout;.isDrawerOpen (Landroid/view/View;)Z
05-20 22:59:27.727: W/dalvikvm(21948): Unable to resolve superclass of Lcom/example/facecliq/HomeActivity$1; (30)
05-20 22:59:27.737: W/dalvikvm(21948): Link of class 'Lcom/example/facecliq/HomeActivity$1;' failed
05-20 22:59:27.757: W/dalvikvm(21948): threadid=1: thread exiting with uncaught exception (group=0x40a9e1f8)
05-20 22:59:27.777: E/AndroidRuntime(21948): FATAL EXCEPTION: main
05-20 22:59:27.777: E/AndroidRuntime(21948): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.facecliq/com.example.facecliq.HomeActivity}: android.view.InflateException: Binary XML file line #1: Error inflating class android.support.v4.widget.DrawerLayout
05-20 22:59:27.777: E/AndroidRuntime(21948): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-20 22:59:27.777: E/AndroidRuntime(21948): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-20 22:59:27.777: E/AndroidRuntime(21948): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-20 22:59:27.777: E/AndroidRuntime(21948): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-20 22:59:27.777: E/AndroidRuntime(21948): at android.os.Handler.dispatchMessage(Handler.java:99)
05-20 22:59:27.777: E/AndroidRuntime(21948): at android.os.Looper.loop(Looper.java:137)
05-20 22:59:27.777: E/AndroidRuntime(21948): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-20 22:59:27.777: E/AndroidRuntime(21948): at java.lang.reflect.Method.invokeNative(Native Method)
05-20 22:59:27.777: E/AndroidRuntime(21948): at java.lang.reflect.Method.invoke(Method.java:511)
05-20 22:59:27.777: E/AndroidRuntime(21948): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:812)
05-20 22:59:27.777: E/AndroidRuntime(21948): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:579)
05-20 22:59:27.777: E/AndroidRuntime(21948): at dalvik.system.NativeStart.main(Native Method)
05-20 22:59:27.777: E/AndroidRuntime(21948): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class android.support.v4.widget.DrawerLayout
05-20 22:59:27.777: E/AndroidRuntime(21948): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
05-20 22:59:27.777: E/AndroidRuntime(21948): at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
05-20 22:59:27.777: E/AndroidRuntime(21948): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
05-20 22:59:27.777: E/AndroidRuntime(21948): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
05-20 22:59:27.777: E/AndroidRuntime(21948): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
05-20 22:59:27.777: E/AndroidRuntime(21948): at android.app.Activity.setContentView(Activity.java:1835)
05-20 22:59:27.777: E/AndroidRuntime(21948): at com.example.facecliq.HomeActivity.onCreate(HomeActivity.java:40)
05-20 22:59:27.777: E/AndroidRuntime(21948): at android.app.Activity.performCreate(Activity.java:4465)
05-20 22:59:27.777: E/AndroidRuntime(21948): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-20 22:59:27.777: E/AndroidRuntime(21948): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-20 22:59:27.777: E/AndroidRuntime(21948): ... 11 more
05-20 22:59:27.777: E/AndroidRuntime(21948): Caused by: java.lang.ClassNotFoundException: android.support.v4.widget.DrawerLayout
05-20 22:59:27.777: E/AndroidRuntime(21948): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
05-20 22:59:27.777: E/AndroidRuntime(21948): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-20 22:59:27.777: E/AndroidRuntime(21948): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-20 22:59:27.777: E/AndroidRuntime(21948): at android.view.LayoutInflater.createView(LayoutInflater.java:552)
05-20 22:59:27.777: E/AndroidRuntime(21948): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
05-20 22:59:27.777: E/AndroidRuntime(21948): ... 20 more