我正在尝试让ListView工作,但我在屏幕上收到一条需要关闭应用程序的消息!我正在寻找错误,但我找不到任何错误!也许它在布局中?我不确定我是否应该在里面有一个ListView或者是否创建了这个?我究竟做错了什么?我也将使用onClick方法,但我想在这种情况下这是一个矿工问题!?提供一些帮助!谢谢!
public class Activity_3 extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone._ID};
// Get a cursor with all people
Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI,
projection, null, null, null);
startManagingCursor(cursor);
ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.activity_3, cursor, new String[] {ContactsContract.Contacts.DISPLAY_NAME}, new int[] {R.id.contactItem });
setListAdapter(adapter);
}
}
布局
<?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="vertical" >
<ListView
android:id="@+id/contactList"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<TextView android:id="@+id/contactItem" >
</TextView>
</LinearLayout>
答案 0 :(得分:1)
您是否已添加此权限
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
manifest.xml中的
好了,现在这应该是你的活动
public class mainact extends Activity {
ListView l;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.abc);
l=(ListView)findViewById(R.id.contactList);
String[] projection = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone._ID};
// Get a cursor with all people
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection,null,null, null);
while (phones.moveToNext())
{
String Name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String Number=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
startManagingCursor(phones);
ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.abc, phones, new String[] {ContactsContract.Contacts.DISPLAY_NAME}, new int[] {R.id.contactItem });
l.setAdapter(adapter);
}
}
和xml应该像
<?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="vertical" >
<ListView
android:id="@+id/contactList"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/contactItem" >
</TextView>
</LinearLayout>
或者只是适合你需要,但这是有效的代码
答案 1 :(得分:0)
据我所知,您正在尝试使用适配器创建视图,其中每个视图包含另一个ListView?
删除
<ListView
android:id="@+id/contactList"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
通常你不能将ListView放在另一个ListView中。