NavigationDrawer with ActionBarSherlok

倾然丶 夕夏残阳落幕 提交于 2019-12-25 10:49:33

问题


Hey I am trying to use DrawerLayout with ActionBarSherlok but i am getting error

06-30 15:37:18.874: E/AndroidRuntime(1305): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Priyesh/com.Priyesh.MainActivity}:android.view.InflateException: Binary XML file line #1: Error inflating class android.support.v4.widget.DrawerLayout

here is my xml file

<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>

and

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);
}

}


回答1:


I just got this running on my project, which was an even more major pain because I'm using Maven.

Don't include the support library in your project, but replace the support library in the ActionBarSherlock lib folder, which is r7, with the newest r13 version. Others have said that you need to include private libraries in the order and export as well, which was not true for me because I'm using Maven.

This tutorial was helpful in getting ABS and DrawerLayout to work together.




回答2:


Try this and let me know if it doesn't solve the issue :

  • Go to Build Path > Configure Build Path > Order and Export
  • Check Android and Android Dependencies libraries
  • Clean and rebuild your Project



回答3:


Do you have the latest support library installed? The class android.support.v4.widget.DrawerLayout is only in the latest library from around a month ago (version 13).




回答4:


I had the same problem. So I solved it using the following steps. It ll be useful to others.

The problem is because the compiler searching drawer_layout in the activity_main.xml.

setContentView(R.layout.activity_main); 
//You added the view file as activity_main 

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
//While execution the compiler will search "drawer_layout" in the view file (activity_main.xml). But the drawer_layout is in some other xml file.

But there is no "drawer_layout" is added in the activity_main.xml. Its added in some other xml file

So, To solve this problem we need to add the following in the activity_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"
    android:background="@drawable/bff">

    <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:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>

<!-- The content in the activity_main.xml -->

</android.support.v4.widget.DrawerLayout>


来源:https://stackoverflow.com/questions/17389763/navigationdrawer-with-actionbarsherlok

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!