ListView强制关闭应用程序
我试图让ListView正常工作,但我在屏幕上看到需要关闭应用程序的消息!我正在寻找错误,但我找不到任何!也许它在布局内部?我不知道如果我应该有一个ListView里面或如果这是创建的cdynamic?我究竟做错了什么?我也打算使用onClick方法,但在这种情况下,我猜这是一个矿工问题!?请提供一些帮助!谢谢!ListView强制关闭应用程序
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>
有你在的manifest.xml添加此权限
<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
确定现在,这应该是你的活动
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>
或只是适合你需要,但这是工作代码
是的!这已经完成了! – 2013-02-22 12:54:44
这就是为什么你需要发布错误日志,我们不知道它为什么崩溃。它可能是任何东西。 – Timmetje 2013-02-22 12:57:10
感谢您的帮助! – 2013-02-22 14:52:59
据我所知,你要使用适配器创建视图,其中每个视图包含其他的ListView?
删除
<ListView
android:id="@+id/contactList"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
通常情况下,你不能把ListView控件在其他的ListView。
不,我不这么认为:)我只想得到所有联系人名单的列表,并能够选择姓名和号码 – 2013-02-22 12:53:56
请尝试发布您的DDMS错误日志。打开你的logcat窗口并为你的应用程序添加一个过滤器。然后你可以看到错误。此外你的布局xml似乎不完整。你在用什么IDE? – Timmetje 2013-02-22 12:38:36
其中是Url适配器类,它扩展了SimpleCursorAdapter或baseadapter – saran 2013-02-22 12:47:54
如果您正在扩展List Activity,那么您不需要在布局中放置一个ListView。 – Anukool 2013-02-22 12:48:13