嘿,我正在尝试将DrawerLayout与ActionBarSherlok一起使用,但出现错误
06-30 15:37:18.874: E/AndroidRuntime(1305): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.Priyesh/com.Priyesh.MainActivity}:android.view.InflateException: 二进制 XML 文件行 # 1: 膨胀类 android.support.v4.widget.DrawerLayout 时出错
这是我的 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/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>
和
package com.example;
import java.util.ArrayList;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.actionbarsherlock.app.*;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
public class MainActivity extends SherlockActivity {
DrawerLayout mydrawerlayout;
ListView drawerlist;
ArrayList<String> mplanetlist;
ActionBarDrawerToggle drawer_toggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydrawerlayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerlist = (ListView) findViewById(R.id.left_drawer);
drawerlist.setAdapter(newArrayAdapter<String(MainActivity.this,android.R.layout.sim ple_list_item_2,mplanetlist));
ActionBar actionbar = getSupportActionBar();
actionbar.show();
actionbar.setHomeButtonEnabled(true);
actionbar.setDisplayHomeAsUpEnabled(true);
drawer_toggle=newActionBarDrawerToggle(MainActivity.this,mydrawerlayout,R.drawable. ic_drawer, R.string.drawer_open , R.string.drawer_close )
{
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle("drawer_open");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle("drawer_closse");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mydrawerlayout.setDrawerListener(drawer_toggle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater m = getSupportMenuInflater();
m.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
return super.onPrepareOptionsMenu(menu);
}
}