0

このリンクで次の例を見つけましたが、実行すると強制的に閉じられます。理由がわかりません。この問題について助けていただけますか?また、人について少し説明します。取り消し線があり、そこにあります。 People が非推奨であるという警告です! 私はAndroidとJavaのプログラミングが本当に初めてです。助けてください。どんな提案でも大歓迎です。

コードは次のとおりです:
list.xml

        <?xml version="1.0" encoding="utf-8"?>
        <!-- This file is at /res/layout/list.xml -->
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical"
            android:layout_width="fill_parent"  android:layout_height="fill_parent">

            <ListView android:id="@android:id/list"
                android:layout_width="fill_parent"  android:layout_height="0dip"
                android:layout_weight="1" />

            <Button android:id="@+id/btn" android:onClick="doClick"
             android:layout_width="wrap_content"  android:layout_height="wrap_content"
             android:text="Submit Selection" />

        </LinearLayout>

テストクラス

 package com.example.multicolumnlistview;    
    import android.app.ListActivity;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.provider.Contacts.People;
    import android.util.Log;
    import android.util.SparseBooleanArray;
    import android.view.View;
    import android.widget.ListView;
    import android.widget.SimpleCursorAdapter;     
    public class Test extends ListActivity
    {
        private static final String TAG = "ListViewActivity3";
        private ListView lv = null;
        private Cursor cursor = null;
        private int idCol = -1;
        private int nameCol = -1;
        private int notesCol = -1;     
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);     
            lv = getListView();             
            cursor = managedQuery(People.CONTENT_URI, 
                            null, null, null, People.NAME);     
            String[] cols = new String[]{People.NAME};
            idCol = cursor.getColumnIndex(People._ID);
            nameCol = cursor.getColumnIndex(People.NAME);
            notesCol = cursor.getColumnIndex(People.NOTES);             
            int[] views = new int[]{android.R.id.text1};             
            SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_list_item_multiple_choice,
                cursor, cols, views);             
            this.setListAdapter(adapter);             
            lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        }         
        public void doClick(View view) {
            int count=lv.getCount();
            SparseBooleanArray viewItems = lv.getCheckedItemPositions();
            for(int i=0; i<count; i++) {
              if(viewItems.get(i)) {
                cursor.moveToPosition(i);
                long id = cursor.getLong(idCol);
                String name = cursor.getString(nameCol);
                String notes = cursor.getString(notesCol);
                Log.v(TAG, name + " is checked. Notes: " + notes +
                    ". Position = " + i + ". Id = " + id);
              }
            }
        }
    }

そしてここにlogcatがあります:

02-04 09:04:59.609: E/DatabaseUtils(99):    at android.os.Binder.execTransact(Binder.java:287)
02-04 09:04:59.609: E/DatabaseUtils(99):    at dalvik.system.NativeStart.run(Native Method)
02-04 09:04:59.619: D/ddm-heap(139): Got feature list request
02-04 09:04:59.628: D/AndroidRuntime(220): Shutting down VM
02-04 09:04:59.628: W/dalvikvm(220): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
02-04 09:04:59.628: E/AndroidRuntime(220): Uncaught handler: thread main exiting due to uncaught exception
02-04 09:04:59.639: E/AndroidRuntime(220): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.multicolumnlistview/com.example.multicolumnlistview.Test}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://contacts/people from pid=220, uid=10029 requires android.permission.READ_CONTACTS
02-04 09:04:59.639: E/AndroidRuntime(220):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
02-04 09:04:59.639: E/AndroidRuntime(220):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
02-04 09:04:59.639: E/AndroidRuntime(220):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
02-04 09:04:59.639: E/AndroidRuntime(220):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
02-04 09:04:59.639: E/AndroidRuntime(220):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-04 09:04:59.639: E/AndroidRuntime(220):  at android.os.Looper.loop(Looper.java:123)
02-04 09:04:59.639: E/AndroidRuntime(220):  at android.app.ActivityThread.main(ActivityThread.java:4363)
02-04 09:04:59.639: E/AndroidRuntime(220):  at java.lang.reflect.Method.invokeNative(Native Method)
02-04 09:04:59.639: E/AndroidRuntime(220):  at java.lang.reflect.Method.invoke(Method.java:521)
02-04 09:04:59.639: E/AndroidRuntime(220):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-04 09:04:59.639: E/AndroidRuntime(220):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-04 09:04:59.639: E/AndroidRuntime(220):  at dalvik.system.NativeStart.main(Native Method)
02-04 09:04:59.639: E/AndroidRuntime(220): Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://contacts/people from pid=220, uid=10029 requires android.permission.READ_CONTACTS
02-04 09:04:59.639: E/AndroidRuntime(220):  at android.os.Parcel.readException(Parcel.java:1218)
02-04 09:04:59.639: E/AndroidRuntime(220):  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160)
02-04 09:04:59.639: E/AndroidRuntime(220):  at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
02-04 09:04:59.639: E/AndroidRuntime(220):  at android.content.ContentProviderProxy.bulkQuery(ContentProviderNative.java:326)
02-04 09:04:59.639: E/AndroidRuntime(220):  at android.content.ContentProviderProxy.query(ContentProviderNative.java:345)
02-04 09:04:59.639: E/AndroidRuntime(220):  at android.content.ContentResolver.query(ContentResolver.java:202)
02-04 09:04:59.639: E/AndroidRuntime(220):  at android.app.Activity.managedQuery(Activity.java:1495)
02-04 09:04:59.639: E/AndroidRuntime(220):  at com.example.multicolumnlistview.Test.onCreate(Test.java:30)
02-04 09:04:59.639: E/AndroidRuntime(220):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-04 09:04:59.639: E/AndroidRuntime(220):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
02-04 09:04:59.639: E/AndroidRuntime(220):  ... 11 more
02-04 09:04:59.649: I/Process(51): Sending signal. PID: 220 SIG: 3
02-04 09:04:59.659: I/dalvikvm(220): threadid=7: reacting to signal 3
02-04 09:04:59.659: E/dalvikvm(220): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
02-04 09:04:59.739: D/ddm-heap(110): Got feature list request
02-04 09:04:59.769: D/ddm-heap(175): Got feature list request
02-04 09:04:59.779: I/ARMAssembler(51): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x345958:0x345a14] in 388876 ns
02-04 09:04:59.788: I/ARMAssembler(51): generated scanline__00000177:03515104_00001001_00000000 [ 91 ipp] (114 ins) at [0x345a18:0x345be0] in 434133 ns
02-04 09:04:59.849: D/ddm-heap(209): Got feature list request
02-04 09:04:59.979: D/ddm-heap(200): Got feature list request
02-04 09:05:08.918: W/ActivityManager(51): Launch timeout has expired, giving up wake lock!
02-04 09:05:09.202: W/ActivityManager(51): Activity idle timeout for HistoryRecord{44e4c570 com.example.multicolumnlistview/.Test}
4

1 に答える 1

0

デバイスの連絡先を読み取るときは、マニフェスト ファイルに READ_CONTACT 権限を追加する必要があります。

 <uses-permission android:name="android.permission.READ_CONTACTS"/>

これだけが問題だと思います。

編集:

チェックボックス付きのリストビューについては、以下のリンクを確認してください。

1)リンク 1
2)リンク 2
3)リンク 3

于 2013-02-04T05:31:31.853 に答える