2017-04-16 22 views
0

我是android的初學者。我想在ListView中顯示聯繫人列表。我已經在activity_main.xml中有一個ListView,並且我在MainActivity.java中提到了這個。但是,當我運行程序我得到一個錯誤我無法在android中檢索聯繫人?

了java.lang.RuntimeException:無法啓動活動 ComponentInfo {com.example.yuvi.contactsex/com.example.yuvi.contactsex.MainActivity}: 的java .lang.RuntimeException:您的內容必須有一個ListView的ID 屬性爲「android.R.id.list」

我從網站的代碼。我不知道在哪裏更改代碼。任何人都幫助我。

  • 的AndroidManifest.xml

    <uses-permission android:name="android.permission.READ_CONTACTS"/> 
    
    <application 
        android:allowBackup="true" 
        android:icon="@mipmap/ic_launcher" 
        android:label="@string/app_name" 
        android:roundIcon="@mipmap/ic_launcher_round" 
        android:supportsRtl="true" 
        android:theme="@style/AppTheme"> 
        <activity android:name=".MainActivity"> 
         <intent-filter> 
          <action android:name="android.intent.action.MAIN" /> 
    
          <category android:name="android.intent.category.LAUNCHER" /> 
         </intent-filter> 
        </activity> 
    </application> 
    

  • activity_main.xml中

  • MainActivity.java

    public class MainActivity extends ListActivity { 
    
    ListView lv; 
    Cursor cursor1; 
    
    @Override 
    public int getSelectedItemPosition() { 
        return super.getSelectedItemPosition(); 
    } 
    
    @Override 
    public long getSelectedItemId() { 
        return super.getSelectedItemId(); 
    } 
    
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
    
        lv=(ListView)findViewById(R.id.list); 
        // create a cursor to query the Contacts on the device to start populating a listview 
        cursor1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); 
        startManagingCursor(cursor1); 
    
        String[] from = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone._ID}; // get the list items for the listadapter could be TITLE or URI 
    
    
        int[] to = {android.R.id.text1, android.R.id.text2}; // sets the items from above string to listview 
    
        // new listadapter, created to use android checked template 
        SimpleCursorAdapter listadapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor1, from, to); 
    
    
        setListAdapter(listadapter); 
    
        // adds listview so I can get data from it 
        lv = getListView(); 
        lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
    
    
        } 
    } 
    

回答

0

了java.lang.RuntimeException:你的內容必須有一個ListView的ID 屬性爲 'android.R.id.list'

由於您使用的是ListActivity您必須添加@androidid屬性的XML:

<ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"/> 

希望這有助於。

+0

我得到了輸出。謝謝。多一個幫助。當用戶單擊列表項時,我需要顯示Toast消息(用戶名)。我可以使用onItemClickListerner嗎?我是否需要使用任何方法? – user3661367

+1

是的,'onItemClickListener'應該這樣做。 :) – tahsinRupam

+0

我的應用程序運行在我的Android模擬器中。但它不能在我的移動設備上運行?爲什麼?我需要修改任何內容嗎? – user3661367

相關問題