尝试使用联系人填充ListView时出现异常

时间:2014-06-06 05:54:56

标签: android listview

我正在尝试使用ListView填充ListViewActivity中的联系人,运行示例的Whil,我得到了一个例外。无法弄清楚原因......

例外是:

06-06 11:14:01.264: E/AndroidRuntime(24017): FATAL EXCEPTION: main
06-06 11:14:01.264: E/AndroidRuntime(24017): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.listviewadapter/com.example.listviewadapter.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
06-06 11:14:01.264: E/AndroidRuntime(24017):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-06 11:14:01.264: E/AndroidRuntime(24017):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-06 11:14:01.264: E/AndroidRuntime(24017):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-06 11:14:01.264: E/AndroidRuntime(24017):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-06 11:14:01.264: E/AndroidRuntime(24017):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-06 11:14:01.264: E/AndroidRuntime(24017):    at android.os.Looper.loop(Looper.java:137)
06-06 11:14:01.264: E/AndroidRuntime(24017):    at android.app.ActivityThread.main(ActivityThread.java:4745)
06-06 11:14:01.264: E/AndroidRuntime(24017):    at java.lang.reflect.Method.invokeNative(Native Method)
06-06 11:14:01.264: E/AndroidRuntime(24017):    at java.lang.reflect.Method.invoke(Method.java:511)
06-06 11:14:01.264: E/AndroidRuntime(24017):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-06 11:14:01.264: E/AndroidRuntime(24017):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-06 11:14:01.264: E/AndroidRuntime(24017):    at dalvik.system.NativeStart.main(Native Method)
06-06 11:14:01.264: E/AndroidRuntime(24017): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
06-06 11:14:01.264: E/AndroidRuntime(24017):    at android.app.ListActivity.onContentChanged(ListActivity.java:243)
06-06 11:14:01.264: E/AndroidRuntime(24017):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:259)
06-06 11:14:01.264: E/AndroidRuntime(24017):    at android.app.Activity.setContentView(Activity.java:1867)
06-06 11:14:01.264: E/AndroidRuntime(24017):    at com.example.listviewadapter.MainActivity.onCreate(MainActivity.java:27)
06-06 11:14:01.264: E/AndroidRuntime(24017):    at android.app.Activity.performCreate(Activity.java:5008)
06-06 11:14:01.264: E/AndroidRuntime(24017):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-06 11:14:01.264: E/AndroidRuntime(24017):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-06 11:14:01.264: E/AndroidRuntime(24017):    ... 11 more

我的活动课程是:

package com.example.listviewadapter;

import android.app.ListActivity;
import android.app.LoaderManager.LoaderCallbacks;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class MainActivity extends ListActivity implements LoaderCallbacks<Cursor>{

SimpleCursorAdapter mAdapter;
static final String[] PROJECTION = new String[] {ContactsContract.Data._ID,
    ContactsContract.Data.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER};
static final String SELECTION = "((" + 
        ContactsContract.Data.DISPLAY_NAME + " NOTNULL) AND (" +
        ContactsContract.Data.DISPLAY_NAME + " != '' ))";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String[] fromcolumns = {ContactsContract.Data.DISPLAY_NAME};
    int[] toViews = {R.id.textView1};

    mAdapter = new SimpleCursorAdapter(this, 
            android.R.layout.simple_list_item_1, null,
            fromcolumns, toViews, 0);
    setListAdapter(mAdapter);
    getLoaderManager().initLoader(0, null, this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    // TODO Auto-generated method stub
    return new CursorLoader(this, ContactsContract.Data.CONTENT_URI,
            PROJECTION, SELECTION, null, null);
}

@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor data) {
    // TODO Auto-generated method stub
     mAdapter.swapCursor(data);
}

@Override
public void onLoaderReset(Loader<Cursor> arg0) {
    // TODO Auto-generated method stub
     mAdapter.swapCursor(null);

}
@Override 
public void onListItemClick(ListView l, View v, int position, long id) {
    // Do something when a list item is clicked
    Toast.makeText(this, position , Toast.LENGTH_LONG).show();
}
}

布局文件是::

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

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

</RelativeLayout>

清单文件中包含Readcontacts权限。

2 个答案:

答案 0 :(得分:1)

您的课程正在延长ListActivity,当您执行此操作时,id的{​​{1}}必须为ListView。如果你想创建listview的对象,你可以android:id="@android:id/list"。您可以像这样ListView lv = getListView();

设置适配器

如果课程正在延长setListAdapter(adapter),您将会写ActivityListView lv = (ListView) findViewById (R.id.listvew1)

更改您的listview ID

lv.setAdapter(adapter)

android:id="@+id/list"

答案 1 :(得分:0)

你需要

 <ListView android:id="@android:id/list"
你的xml中的

  

ListActivity的默认布局由单个布局组成,   屏幕中央的全屏列表。但是,如果你愿意,   您可以通过设置自己的视图布局来自定义屏幕布局   在onCreate()中使用setContentView()。要做到这一点,你自己的观点必须   包含一个ID为#34; @android:id / list&#34;的ListView对象; (或列出如果   它在代码中)