0

我有一個navigation Drawer在我的App.Now我需要使抽屜出現在我的所有activities如何在所有活動中添加導航抽屜

我看到許多相似的問題,這&找到了解決https://stackoverflow.com/a/4922740/2781359

但我想它在我的App.Then它拋出一個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> 
+0

是否使用碎片而不是替代?如果是這樣,我強烈建議你使用片段,因爲它們更「靈活」。 – Endzeit

+0

java.lang.UnsupportedOperationException:addView(View,LayoutParams)在AdapterView中不受支持您是否發現此錯誤的任何解決方案 –

回答

0

您是鑄造了錯誤的佈局ID的

試試這個

public abstract class MainActivity extends ActionBarActivity 
{ 
    protected DrawerLayout fullLayout; 
    protected ListView actContent; 
    private ListView mDrawerList; 
    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); 
    } 

編輯完整的解決方案:

main_content.xml

<?xml version="1.0" encoding="utf-8"?> 

<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/act_content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 


    <RelativeLayout 
     android:id="@+id/left_drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start"> 

     <ListView 
      android:layout_below="@+id/drawer_list" 
      android:layout_width="240dp" 
      android:id="@+id/filters_list" 
      android:layout_height="wrap_content"/> 
    </RelativeLayout>  
</android.support.v4.widget.DrawerLayout> 

乙aseActivity

protected DrawerLayout fullLayout; 
protected FrameLayout actContent; 
private ActionBarDrawerToggle mDrawerToggle; 
ListView mDrawerList; 


@Override 
public void setContentView(final int layoutResID) { 


    fullLayout= (DrawerLayout) getLayoutInflater().inflate(R.layout.main_content, null); // Your base layout here 


    mDrawerList = (ListView)fullLayout.findViewById(R.id.drawer_list); 
    fullLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,mFilterList); 
    fullLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); 

    int drawer_icon = R.drawable.ic_drawer; 
    mDrawerToggle = new ActionBarDrawerToggle(
      this, 
      fullLayout, 
      drawer_icon, 
      R.string.drawer_open, 
     R.string.drawer_close 
     ) { 
     public void onDrawerClosed(View view) { 
      invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
     } 

     public void onDrawerOpened(View drawerView) { 
      invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu() 
     } 
    }; 
    fullLayout.setDrawerListener(mDrawerToggle); 

    actContent= (FrameLayout) fullLayout.findViewById(R.id.act_content); 
    getLayoutInflater().inflate(layoutResID, actContent, true); // Setting the content of layout your provided to the act_content frame 

    super.setContentView(fullLayout); 
} 

@Override 
protected void onPostCreate(Bundle savedInstanceState) { 
    super.onPostCreate(savedInstanceState); 
    // Sync the toggle state after onRestoreInstanceState has occurred. 
    mDrawerToggle.syncState(); 
} 

感謝

+0

已更正It.But發生錯誤。請參閱更新的Logcat。 – AruLNadhaN

+0

@ArulNadhan剛剛編輯完整工作解決方案的答案 – eshbeata

+0

顯示此異常java.lang.RuntimeException:無法啓動活動ComponentInfo {com.ControllerDroid.client/com.ControllerDroid.client.activity.MainActivity}:java.lang.ClassCastException: android.widget.RelativeLayout不能轉換爲android.support.v4.widget.DrawerLayout – AruLNadhaN

1

MainActivityabstract,不能被實例化。

如果您打算保持抽象,請從清單中刪除它的條目,並確保您沒有試圖用明確的Intent實例化。

+0

編輯爲建議。現在它顯示UnsupportedOperationException? – AruLNadhaN

+0

您仍然試圖按照MainActivity.onCreate在堆棧跟蹤中所建議的那樣實例化MainActivity。 – laalto

+0

我從清單中刪除它沒有意圖引用它.... LogCat更新... – AruLNadhaN