我的应用程序中有一个navigation Drawer
。现在我需要让那个抽屉出现在我所有的activities
.
我看到了许多与此类似的问题并找到了解决方案https://stackoverflow.com/a/4922740/2781359
但我在我的应用程序中尝试过它。然后它抛出一个Exception
.
所以帮助我朝着正确的方向前进:)
感谢您的帮助...
Logcat
01-25 22:35:14.062: E/AndroidRuntime(17091): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ControllerDroid.client/com.ControllerDroid.client.activity.ControlActivity}: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
01-25 22:35:14.062: E/AndroidRuntime(17091): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215)
01-25 22:35:14.062: E/AndroidRuntime(17091): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
01-25 22:35:14.062: E/AndroidRuntime(17091): at android.app.ActivityThread.access$800(ActivityThread.java:145)
01-25 22:35:14.062: E/AndroidRuntime(17091): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
01-25 22:35:14.062: E/AndroidRuntime(17091): at android.os.Handler.dispatchMessage(Handler.java:102)
01-25 22:35:14.062: E/AndroidRuntime(17091): at android.os.Looper.loop(Looper.java:136)
01-25 22:35:14.062: E/AndroidRuntime(17091): at android.app.ActivityThread.main(ActivityThread.java:5081)
01-25 22:35:14.062: E/AndroidRuntime(17091): at java.lang.reflect.Method.invokeNative(Native Method)
01-25 22:35:14.062: E/AndroidRuntime(17091): at java.lang.reflect.Method.invoke(Method.java:515)
01-25 22:35:14.062: E/AndroidRuntime(17091): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
01-25 22:35:14.062: E/AndroidRuntime(17091): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-25 22:35:14.062: E/AndroidRuntime(17091): at dalvik.system.NativeStart.main(Native Method)
01-25 22:35:14.062: E/AndroidRuntime(17091): Caused by: java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView
01-25 22:35:14.062: E/AndroidRuntime(17091): at android.widget.AdapterView.addView(AdapterView.java:478)
01-25 22:35:14.062: E/AndroidRuntime(17091): at android.view.LayoutInflater.inflate(LayoutInflater.java:500)
01-25 22:35:14.062: E/AndroidRuntime(17091): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-25 22:35:14.062: E/AndroidRuntime(17091): at com.ControllerDroid.client.activity.MainActivity.setContentView(MainActivity.java:37)
01-25 22:35:14.062: E/AndroidRuntime(17091): at com.ControllerDroid.client.activity.MainActivity.onCreate(MainActivity.java:56)
01-25 22:35:14.062: E/AndroidRuntime(17091): at com.ControllerDroid.client.activity.ControlActivity.onCreate(ControlActivity.java:62)
01-25 22:35:14.062: E/AndroidRuntime(17091): at android.app.Activity.performCreate(Activity.java:5231)
01-25 22:35:14.062: E/AndroidRuntime(17091): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-25 22:35:14.062: E/AndroidRuntime(17091): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
01-25 22:35:14.062: E/AndroidRuntime(17091): ... 11 more
MainActivity.java
public class MainActivity extends ActionBarActivity
{
protected DrawerLayout fullLayout;
protected ListView actContent;
private ListView mDrawerList;
private DrawerLayout mDrawer;
private CustomActionBarDrawerToggle mDrawerToggle;
private String[] menuItems;
@Override
public void setContentView(final int layoutResID)
{
fullLayout = (DrawerLayout)getLayoutInflater().inflate(R.layout.activity_main_drawer, null); // Your base layout here
ListView actContent = (ListView) fullLayout.findViewById(R.id.drawer);
getLayoutInflater().inflate(layoutResID, actContent, true); // Setting the content of layout your provided to the act_content frame
super.setContentView(fullLayout);
// here you can get your drawer buttons and define how they should
// behave and what must they do, so you won't be needing to repeat it in
// every activity class
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
// set a custom shadow that overlays the main content when the drawer
// opens
fullLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
_initMenu();
mDrawerToggle = new CustomActionBarDrawerToggle(this, fullLayout);
fullLayout.setDrawerListener(mDrawerToggle);
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_drawer);
}
activity_main_drawer.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" >
<!-- The main content view -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/text_color"
android:text="@string/drawer_text" />
</RelativeLayout>
<!-- The navigation drawer -->
<ListView
android:id="@+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:cacheColorHint="#00000000"
android:background="@color/list_background"
android:choiceMode="singleChoice"
android:textAppearance="@android:style/TextAppearance.Medium"
android:divider="@android:color/transparent"
android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>