Frage

So i'm trying to learn about the DrawerLayout. first, I just created it and it was working good. and then I tried to manipulate it by adding the icon on the top left, that allows you to show/hide the list. The problem is that when I launched the application, the list was already shown, and when I tried to hide it by clicking on the icon, the application crashed. Here are my files: Activity_main.xml:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/DrawerLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/ContenuFrame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

       <ListView
           android:id="@+id/ListView1"
            android:layout_width="241dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="#666"
            android:dividerHeight="1dp"
            android:background="#333"
            android:paddingLeft="15sp"
            android:paddingRight="15sp"/>
    </FrameLayout>


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

Item_Layout: (Layout for the Items (each one) in the list):

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

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/Element"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#fff"
        android:textSize="20sp"
        android:gravity="center_vertical"
        android:paddingStart="14.5sp"
        android:paddingEnd="14.5sp"
        android:minHeight="35sp"/>

MainActivity.java:

package com.example.drawerlisttest;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

    String[] ListElement;
    ListView List;
    DrawerLayout DLayout;
    ActionBarDrawerToggle ABDT;

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

        ListElement = getResources().getStringArray(R.array.StringList);
        List = (ListView)findViewById(R.id.ListView1);
        List.setAdapter(new ArrayAdapter<String>(this, R.layout.item_layout, ListElement));

        //Set the Icon:
        DLayout = (DrawerLayout)findViewById(R.id.DrawerLayout1);
        ABDT = new ActionBarDrawerToggle(this, DLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close);

        //Faire de l'actionBarDrawerToggle un DrawerListener de DrawerLayout
        DLayout.setDrawerListener(ABDT);
        getActionBar().setDisplayHomeAsUpEnabled(true);

        //Add the shodow to the right after displying the list
        DLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START); 
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig){
        super.onConfigurationChanged(newConfig);
        ABDT.onConfigurationChanged(newConfig);
    }

     @Override
        public boolean onOptionsItemSelected(MenuItem item) {

             // call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns true
            // then it has handled the app icon touch event
            if (ABDT.onOptionsItemSelected(item)) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }
}

and finally the Log:

01-25 18:52:53.232: E/AndroidRuntime(1618): FATAL EXCEPTION: main
01-25 18:52:53.232: E/AndroidRuntime(1618): Process: com.example.drawerlisttest, PID: 1618
01-25 18:52:53.232: E/AndroidRuntime(1618): java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
01-25 18:52:53.232: E/AndroidRuntime(1618):     at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1046)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at android.support.v4.app.ActionBarDrawerToggle.onOptionsItemSelected(ActionBarDrawerToggle.java:292)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at com.example.drawerlisttest.MainActivity.onOptionsItemSelected(MainActivity.java:61)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at android.app.Activity.onMenuItemSelected(Activity.java:2600)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at com.android.internal.widget.ActionBarView$3.onClick(ActionBarView.java:167)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at android.view.View.performClick(View.java:4438)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at android.view.View$PerformClick.run(View.java:18422)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at android.os.Handler.handleCallback(Handler.java:733)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at android.os.Handler.dispatchMessage(Handler.java:95)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at android.os.Looper.loop(Looper.java:136)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at android.app.ActivityThread.main(ActivityThread.java:5017)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at java.lang.reflect.Method.invokeNative(Native Method)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at java.lang.reflect.Method.invoke(Method.java:515)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-25 18:52:53.232: E/AndroidRuntime(1618):     at dalvik.system.NativeStart.main(Native Method)

As you can see, the Log says something about some gravity, I looked for it in the layout files and there's no such Node with gravity = left or something like that...

As for the string.xml, well; I don't think that it's necessary important ...

Thank you.

War es hilfreich?

Lösung

Try remove your listView from inside your FrameLayout.

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/DrawerLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
    android:id="@+id/ContenuFrame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

<ListView
    android:id="@+id/ListView1"
    android:layout_width="241dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="#666"
    android:dividerHeight="1dp"
    android:background="#333"
    android:paddingLeft="15sp"
    android:paddingRight="15sp"/>

</android.support.v4.widget.DrawerLayout>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top