سؤال

I am using the navigation drawer in my app. Above the listview, I have used edittext to make a search bar that will search the listview. But now, when I click the list items, instead of opening other activities, my app force closes. What's wrong?

Here is the stack trace:

01-21 18:36:28.410    4151-4184/? I/InputReader﹕ dispatchTouch::touch event's action is 0
01-21 18:36:28.410    4151-4183/? I/InputDispatcher﹕ Delivering touch to current input     target: action: 0, channel '40a1ba50     com.Chinmay.navigationdrawer/com.Chinmay.navigationdrawer.MainActivity (server)'
01-21 18:36:28.426    4151-4184/? I/InputReader﹕ dispatchTouch::touch event's action is 1
01-21 18:36:28.426    4151-4183/? I/InputDispatcher﹕ Delivering touch to current input target: action: 1, channel '40a1ba50 com.Chinmay.navigationdrawer/com.Chinmay.navigationdrawer.MainActivity (server)'
01-21 18:36:28.574  18048-18048/com.Chinmay.navigationdrawer D/AndroidRuntime﹕ Shutting down VM
01-21 18:36:28.574  18048-18048/com.Chinmay.navigationdrawer W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40018578)
01-21 18:36:28.582    4151-4304/? E/liblog﹕ failed to call dumpstate
01-21 18:36:28.582    4151-4165/? E/﹕ Dumpstate > /data/log/dumpstate_app_error
01-21 18:36:28.582  18048-18048/com.Chinmay.navigationdrawer E/AndroidRuntime﹕ FATAL     EXCEPTION: main
    java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
    at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:844)
    at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1048)
    at com.Chinmay.navigationdrawer.MainActivity$1.onItemClick(MainActivity.java:43)
    at android.widget.AdapterView.performItemClick(AdapterView.java:284)
    at android.widget.ListView.performItemClick(ListView.java:3736)

Java

public class MainActivity extends FragmentActivity {
final String[] data ={"Aluminium","Gold","Zinc"};

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

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);

    final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
    final ListView navList = (ListView) findViewById(R.id.left_drawer);
    navList.setAdapter(adapter);
    navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){

            switch (pos){
                case 0:
                    Intent i = new Intent(MainActivity.this,Aluminium.class);
                    startActivity(i);
                    drawer.closeDrawer(navList);
                    break;
                case 1:
                    Intent i2 = new Intent(MainActivity.this,Gold.class);
                    startActivity(i2);
                    drawer.closeDrawer(navList);
                    break;
                case 2:
                    Intent i3 = new Intent(MainActivity.this,Zinc.class);
                    startActivity(i3);
                    drawer.closeDrawer(navList);
                    break;
            }
        }
    });
  }
}

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 -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<!-- The navigation drawer -->
<LinearLayout
    android:id="@+id/left_drawer_layout"
    android:layout_height="wrap_content"
    android:layout_width="240dp"
    android:orientation="vertical"
    android:layout_gravity="start" >
    <EditText
        android:id="@+id/EditText01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#111"
        android:hint="Search" >
    </EditText>
    <ListView android:id="@+id/left_drawer"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</LinearLayout>

هل كانت مفيدة؟

المحلول

Here LinearLayout is Drawer View not ListView. You have to pass instance of LinearLayout to closeDrawer()...

    final LinearLayout linearLayout = (LinearLayout)findViewById(R.id.left_drawer_layout);
    navList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){

            switch (pos){
                case 0:
                    Intent i = new Intent(MainActivity.this,Aluminium.class);
                    startActivity(i);
                    break;
                case 1:
                    Intent i2 = new Intent(MainActivity.this,Gold.class);
                    startActivity(i2);
                    break;
                case 2:
                    Intent i3 = new Intent(MainActivity.this,Zinc.class);
                    startActivity(i3);
                    break;
            }
            drawer.closeDrawer(linearLayout);
        }
    });

نصائح أخرى

change:

drawer.closeDrawer(navList);

to:

 drawer.closeDrawer(Gravity.START);

Your ListView(navList) with id left_drawer is not drawer LinearLayout with id left_drawer_layout.

So either pass the object of this linearlayout or you can also close drawer by direction like the above.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top