我正在尝试创建一个用于管理不同路径的活动。在左侧应该有一个侧边栏,您可以选择不同的课程。在右侧,将显示相应的pathattributes。
当我长时间点击某个项目时,应启动上下文操作模式。在那里,您可以选择多个项目,并可以执行一些批处理操作,例如一次更新更多的托管或从列表中删除它们。
问题是,每当我在动作模式下更改模拟器或真实设备的方向时,应用程序都会崩溃。我尝试了不同的方式来完成模式(mode.finish,在onSaveInstances上使用布尔值...)但模式总是在onCreate恢复。
另一个奇怪的行为是指PathSimpleCurosrAdapter。这是一个简单的SimpleCursorAdapter,它根据它们的基础数据和第一个孩子光线的颜色来编辑ListView孩子。 没有MulitChoiceListener一切正常,但有了它我不能通过我的普通onItemClickfunction改变第一个元素的颜色。
我的主要活动
public class MainActivity extends Activity {
private int current_clicked=0; // marks last clicked item at lv_pahtlist
private ListView lv_pathlist;
private ListView lv_pathdetail;
private SQLiteDatabase db;
private boolean isInActionMode;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv_pathlist =(ListView) findViewById (R.id.listview_main);
lv_pathdetail=(ListView) findViewById (R.id.listview_path);
// SQLHelper creates or retrieves database
DBZugriff dbhelp=new DBZugriff(this, "Pfaddetails");
db=dbhelp.getWritableDatabase();
Cursor Cursor=db.query("ta_pathlist", new String[]{"pathname, refresh, saved", "_id"},
"", null, null, null, null);
PathSimpleCursorAdapter adapter=new PathSimpleCursorAdapter(getBaseContext(),
R.layout.list_item_img1, Cursor, new String[]{"pathname"}, new int[]{R.id.item_tv_path_detail},0);
lv_pathlist.setAdapter(adapter);
lv_pathlist.setBackgroundColor(Color.GRAY);
Cursor cursor2=db.query("ta_pathattribute", new String[]{"_id","attribute_value"},
"fk_path=1", null, null, null, null);
SimpleCursorAdapter adapter2=new SimpleCursorAdapter(getBaseContext(),
R.layout.list_item2, cursor2, new String[]{"attribute_value"}, new int[]{R.id.list_item2_tv},0);
lv_pathdetail.setAdapter(adapter2);
//Contextmenu
registerForContextMenu(lv_pathlist);
lv_pathlist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
lv_pathlist.setMultiChoiceModeListener(new MultiChoiceModeListener() {
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
if (checked==true)
lv_pathlist.getChildAt(position).setBackgroundColor(Color.LTGRAY);
else
lv_pathlist.getChildAt(position).setBackgroundColor(Color.GRAY);
}
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
MenuInflater inflater = mode.getMenuInflater();
inflater.inflate(R.menu.activity_main_context, menu);
return true;
}
public void onDestroyActionMode(ActionMode mode) {
for(int i=0; i<lv_pathlist.getCount();i++)
lv_pathlist.getChildAt(i).setBackgroundColor(Color.GRAY);
//if possible: mark last clicked element
if(lv_pathlist.getChildAt(current_clicked)!=null)
lv_pathlist.getChildAt(current_clicked).setBackgroundColor(Color.LTGRAY);
}
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
for (int i=0;i<lv_pathlist.getCount();i++)
lv_pathlist.getChildAt(i).setBackgroundColor(Color.GRAY);
SparseBooleanArray checked= lv_pathlist.getCheckedItemPositions();
for(int index = checked.size() - 1; index >= 0; index--)
lv_pathlist.getChildAt(index).setBackgroundColor(Color.LTGRAY);
return false;
}
public boolean onActionItemClicked(ActionMode arg0, MenuItem arg1) {
//Some actions
return false;
}
});
// change background color when item is checked/unchecked
lv_pathlist.setOnItemClickListener(new OnItemClickListener() {
//@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
lv_pathlist.getChildAt(current_clicked).setBackgroundColor(Color.GRAY);
lv_pathlist.getChildAt(position).setBackgroundColor(Color.LTGRAY);
current_clicked=position;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
PathSimpleCursorAdapter
public class PathSimpleCursorAdapter extends SimpleCursorAdapter{
public PathSimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
}
@Override
public void bindView (View view, Context context, Cursor cursor)
{
/*
* Der größte Teil der Arbeit wird von der Vatermethode erledigt
*/
super.bindView(view, context, cursor);
if(cursor.getInt(cursor.getColumnIndex("saved"))==1)
view.findViewById(R.id.item_img_path_detail1).setVisibility(0);
if(cursor.getInt(cursor.getColumnIndex("refresh"))==1)
view.findViewById(R.id.item_img_path_detail2).setVisibility(0);
}
}
活动布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ListView
android:id="@+id/listview_main"
android:layout_weight="2"
android:layout_height="match_parent"
android:layout_width="0dp"
>
</ListView>
<LinearLayout
android:id="@+id/main_right"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="5"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical" >
<ImageView
android:id="@+id/Main_Image"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_margin="20dp"
android:maxHeight="120dp"
android:maxWidth="120dp"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/Button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/Button"/>
<ListView
android:id="@+id/listview_path"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="50dp"
android:listSelector="@android:color/transparent">
</ListView>
</LinearLayout>
</LinearLayout>
项目布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/item_tv_path_detail"
android:layout_weight="8"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:padding="10dp"
android:textSize="20dp"
/>
<ImageView
android:id="@+id/item_img_path_detail1"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:src="@drawable/content_attachment"
android:visibility="invisible"
/>
<ImageView
android:id="@+id/item_img_path_detail2"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:src="@drawable/navigation_refresh"
android:visibility="invisible"
/>
</LinearLayout>
logcat说
11-26 20:23:49.685: E/AndroidRuntime(638): FATAL EXCEPTION: main
11-26 20:23:49.685: E/AndroidRuntime(638): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.hsanhalt.inf.treeducation/de.hsanhalt.inf.treeducation.MainActivity}: java.lang.NullPointerException
11-26 20:23:49.685: E/AndroidRuntime(638): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
11-26 20:23:49.685: E/AndroidRuntime(638): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
11-26 20:23:49.685: E/AndroidRuntime(638): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3351)
11-26 20:23:49.685: E/AndroidRuntime(638): at android.app.ActivityThread.access$700(ActivityThread.java:123)
11-26 20:23:49.685: E/AndroidRuntime(638): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
11-26 20:23:49.685: E/AndroidRuntime(638): at android.os.Handler.dispatchMessage(Handler.java:99)
11-26 20:23:49.685: E/AndroidRuntime(638): at android.os.Looper.loop(Looper.java:137)
11-26 20:23:49.685: E/AndroidRuntime(638): at android.app.ActivityThread.main(ActivityThread.java:4424)
11-26 20:23:49.685: E/AndroidRuntime(638): at java.lang.reflect.Method.invokeNative(Native Method)
11-26 20:23:49.685: E/AndroidRuntime(638): at java.lang.reflect.Method.invoke(Method.java:511)
11-26 20:23:49.685: E/AndroidRuntime(638): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-26 20:23:49.685: E/AndroidRuntime(638): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-26 20:23:49.685: E/AndroidRuntime(638): at dalvik.system.NativeStart.main(Native Method)
11-26 20:23:49.685: E/AndroidRuntime(638): Caused by: java.lang.NullPointerException
11-26 20:23:49.685: E/AndroidRuntime(638): at de.hsanhalt.inf.treeducation.MainActivity$1.onPrepareActionMode(MainActivity.java:126)
11-26 20:23:49.685: E/AndroidRuntime(638): at android.widget.AbsListView$MultiChoiceModeWrapper.onPrepareActionMode(AbsListView.java:5644)
11-26 20:23:49.685: E/AndroidRuntime(638): at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onPrepareActionMode(PhoneWindow.java:2469)
11-26 20:23:49.685: E/AndroidRuntime(638): at com.android.internal.app.ActionBarImpl$ActionModeImpl.invalidate(ActionBarImpl.java:699)
11-26 20:23:49.685: E/AndroidRuntime(638): at com.android.internal.app.ActionBarImpl.startActionMode(ActionBarImpl.java:394)
11-26 20:23:49.685: E/AndroidRuntime(638): at android.app.Activity.onWindowStartingActionMode(Activity.java:4380)
11-26 20:23:49.685: E/AndroidRuntime(638): at com.android.internal.policy.impl.PhoneWindow$DecorView.startActionMode(PhoneWindow.java:2171)
11-26 20:23:49.685: E/AndroidRuntime(638): at com.android.internal.policy.impl.PhoneWindow$DecorView.startActionModeForChild(PhoneWindow.java:2158)
11-26 20:23:49.685: E/AndroidRuntime(638): at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:571)
11-26 20:23:49.685: E/AndroidRuntime(638): at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:571)
11-26 20:23:49.685: E/AndroidRuntime(638): at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:571)
11-26 20:23:49.685: E/AndroidRuntime(638): at android.view.View.startActionMode(View.java:3607)
11-26 20:23:49.685: E/AndroidRuntime(638): at android.widget.AbsListView.onRestoreInstanceState(AbsListView.java:1622)
11-26 20:23:49.685: E/AndroidRuntime(638): at android.view.View.dispatchRestoreInstanceState(View.java:9939)
11-26 20:23:49.685: E/AndroidRuntime(638): at android.view.ViewGroup.dispatchThawSelfOnly(ViewGroup.java:2338)
11-26 20:23:49.685: E/AndroidRuntime(638): at android.widget.AdapterView.dispatchRestoreInstanceState(AdapterView.java:778)
11-26 20:23:49.685: E/AndroidRuntime(638): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2324)
11-26 20:23:49.685: E/AndroidRuntime(638): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2324)
11-26 20:23:49.685: E/AndroidRuntime(638): at android.view.View.restoreHierarchyState(View.java:9917)
11-26 20:23:49.685: E/AndroidRuntime(638): at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1600)
11-26 20:23:49.685: E/AndroidRuntime(638): at android.app.Activity.onRestoreInstanceState(Activity.java:906)
11-26 20:23:49.685: E/AndroidRuntime(638): at android.app.Activity.performRestoreInstanceState(Activity.java:878)
11-26 20:23:49.685: E/AndroidRuntime(638): at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1100)
11-26 20:23:49.685: E/AndroidRuntime(638): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
11-26 20:23:49.685: E/AndroidRuntime(638): ... 12 more