2

さまざまなパスを管理するためのアクティビティを作成しようとしています。左側には、さまざまなコースを選択できるサイドバーがあるはずです。右側には、対応するパス属性が表示されます。

アイテムを長押しすると、コンテキスト アクション モードが開始されます。そこでは、複数のアイテムを選択して、複数のエントリを一度に更新したり、リストから削除したりするなどのバッチ操作を実行できます。

問題は、アクション モードでエミュレーターまたは実際のデバイスの向きを変更すると、アプリがクラッシュすることです。モードを終了する別の方法を試しました (mode.finish、onSaveInstances でブール値を使用して ...) が、モードは常に onCreate で復元されます。

もう 1 つの奇妙な動作は、PathSimpleCurosrAdapter を参照しています。これは通常の SimpleCursorAdapter であり、基になるデータに応じて ListView の子を編集し、最初の子をライトグレーに色付けします。MulitChoiceListener がなければすべて正常に動作しますが、通常の onItemClick 関数で最初の要素の色を変更することはできません。

私のメインアクティビティ

    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>

ログキャットは言う

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
4

0 に答える 0