RelativeLayout $ LayoutParams无法强制转换为android.support.v4.widget.DrawerLayout $ LayoutParams

时间:2016-10-22 07:54:42

标签: android navigation-drawer drawerlayout android-navigation-drawer android-drawer

我正在尝试在Android应用中使用导航drawerLayout,但由于

错误导致我的应用崩溃
drawerLayout.closeDrawer(listviewsliding);

我找不到如何修复它,我需要建议。

错误是:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nabia.myapplication/com.example.nabia.myapplication.Activities.FindNearbyMosque}: java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
                  at android.app.ActivityThread.access$900(ActivityThread.java:177)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:145)
                  at android.app.ActivityThread.main(ActivityThread.java:5942)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
       Caused by: java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
                  at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:1425)
                  at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1676)
                  at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1666)
                  at com.example.nabia.myapplication.Activities.FindNearbyMosque.onCreate(FindNearbyMosque.java:105)
                  at android.app.Activity.performCreate(Activity.java:6289)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 
                  at android.app.ActivityThread.access$900(ActivityThread.java:177) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:145) 
                  at android.app.ActivityThread.main(ActivityThread.java:5942) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372)

我的Java代码是:

public class FindNearbyMosque extends AppCompatActivity{
    private List<ItemSlideMenu> itemSlideMenus;
    private SlidingMenuAdapter slidingMenuAdapter;
    private ListView listviewsliding;
    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle actionBarDrawerToggle;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        //Init component
        listviewsliding = (ListView) findViewById(R.id.listView);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        itemSlideMenus = new ArrayList<>();
        //Add itmes for sliding list
        itemSlideMenus.add(new ItemSlideMenu(R.drawable.time, "Masjid Timing"));
        itemSlideMenus.add(new ItemSlideMenu(R.drawable.mosque, "My Mosque"));
        itemSlideMenus.add(new ItemSlideMenu(R.drawable.settings, "Setting"));
        slidingMenuAdapter = new SlidingMenuAdapter(this, itemSlideMenus);
        listviewsliding.setAdapter(slidingMenuAdapter);

        //Display icon to open/ close sliding list
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        //Set title
        setTitle(itemSlideMenus.get(0).getTitle());
        //Item selected
        listviewsliding.setItemChecked(0, true);
        //Close menu
        drawerLayout.closeDrawer(listviewsliding);

        //Display fragment 1 when start
        replaceFragment(0);

        //Handle on item click
        listviewsliding.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //Set title
                setTitle(itemSlideMenus.get(position).getTitle());
                //Item selected
                listviewsliding.setItemChecked(position, true);
                //Replce Fragment
                replaceFragment(position);
                //close menu
                drawerLayout.closeDrawer(listviewsliding);
            }
        });

        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_opened, R.string.drawer_closed){
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                invalidateOptionsMenu();
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                invalidateOptionsMenu();
            }
        };

        drawerLayout.setDrawerListener(actionBarDrawerToggle);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (actionBarDrawerToggle.onOptionsItemSelected(item))
        {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        actionBarDrawerToggle.syncState();
    }

    //Create method replace fragment

    private void replaceFragment (int pos){
        Fragment fragment = null;
        switch (pos){
            case 0:
                fragment = new MasjidTiming();
                break;
            case 1:
                fragment = new MyMosque();
                break;

            case 2:
                fragment = new Settings();
                break;
            default:
                fragment = new MasjidTiming();
                break;
        }
        if (null!=fragment)
        {
            FragmentManager fragmentManager = getFragmentManager();
            android.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.main_consent, fragment);
            transaction.addToBackStack(null);
            transaction.commit();
        }
    }
}

XML文件是:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout"
    android:orientation="horizontal"
    >

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:id="@+id/main_consent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <ListView
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        android:choiceMode="singleChoice"
        android:layout_gravity="start"
        android:background="#FFFFFF"
        />

</RelativeLayout>

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

的Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.nabia.myapplication">

    <application
        android:name=".Model.GlobalClass"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".Activities.SplachScreen"
            android:theme="@style/SplachScreenTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Activities.FindNearbyMosque" />
        <activity android:name=".Activities.LogIn" />
        <activity android:name=".Activities.SignUP" />
        <activity android:name=".Activities.LocateMe"></activity>
    </application>

</manifest>

4 个答案:

答案 0 :(得分:0)

我已经通过替换RelativeLayout文件中XML的结束标记解决了我的问题,就像这样。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout"
    android:orientation="horizontal"
    >

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:id="@+id/main_consent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</RelativeLayout>
        <ListView
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        android:choiceMode="singleChoice"
        android:layout_gravity="start"
        android:background="#FFFFFF"
        />
</android.support.v4.widget.DrawerLayout>

而不是:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout"
    android:orientation="horizontal"
    >

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:id="@+id/main_consent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <ListView
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        android:choiceMode="singleChoice"
        android:layout_gravity="start"
        android:background="#FFFFFF"
        />

</RelativeLayout>

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

答案 1 :(得分:0)

我已经解决了这个问题。  你使用linearlayout:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_left_drawer"
    android:orientation="vertical"
    android:id="@+id/main_consent"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</LinearLayout>
        <ListView
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        android:choiceMode="singleChoice"
        android:layout_gravity="start"
        android:background="#FFFFFF"
        />
</LinearLayout>

然后在oncreate()

中输入这一行
public LinearLayout mDrawerLinearLayout;
mDrawerLinearLayout = (LinearLayout) findViewById(R.id.main_left_drawer);

分别使用以下代码行打开或关闭抽屉:

drawerLayout.openDrawer(mDrawerLinearLayout);
drawerLayout.closeDrawer(mDrawerLinearLayout);

答案 2 :(得分:0)

你可以添加

私人RelativeLayout DrawerRelative;

DrawerRelative =(RelativeLayout)findViewById(R.id.main_consent);

然后改变

drawerLayout.closeDrawer(listviewsliding);

drawerLayout.closeDrawer(DrawerRelative);

答案 3 :(得分:0)

要关闭抽屉,请尝试使用以下语句:

mDrawerLayout.closeDrawer(GravityCompat.START);