0

我正在尝试drawerLayout在 android 应用程序中使用导航,但我的应用程序由于出现错误而崩溃

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>

清单.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

4 回答 4

0

我已经解决了这个问题。你桅杆使用线性布局:

<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);
于 2017-11-02T17:11:23.947 回答
0

你可以加

私有RelativeLayout DrawerRelative;

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

然后改变

drawLayout.closeDrawer(listviewsliding);

抽屉布局.closeDrawer(DrawerRelative);

于 2017-11-29T12:48:14.427 回答
0

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

mDrawerLayout.closeDrawer(GravityCompat.START);
于 2019-07-15T05:50:46.140 回答
0

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

<?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>
于 2016-10-23T15:08:56.573 回答