我在我的应用中使用导航抽屉。在listview
上方,我使用edittext
制作了一个搜索listview
的搜索栏。但现在,当我点击列表项时,我的应用程序强制关闭,而不是打开其他活动。怎么了?
这是堆栈跟踪:
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)
爪哇
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>
答案 0 :(得分:2)
此处LinearLayout
是抽屉视图而非ListView
。您必须将LinearLayout
的实例传递给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);
}
});
答案 1 :(得分:1)
变化:
drawer.closeDrawer(navList);
为:
drawer.closeDrawer(Gravity.START);
标识为ListView
的{{1}}(navList)不是标识为left_drawer
的抽屉LinearLayout
。
因此要么传递此线性布局的对象,要么也可以按照上面的方向关闭抽屉。