当我在 android 2.1 中运行程序时。我收到错误应用程序已意外停止然后强制关闭。我一次又一次地清理项目然后构建但问题仍然存在。该程序需要在 ListView 上显示来自模拟器的所有联系人的电子邮件地址。
代码
public class Trial3 extends Activity {
ListView lvItem;
private Button btnAdd;
String displayName="", emailAddress="", phoneNumber="";
ArrayList<String> contactlist=new ArrayList<String>();
ArrayAdapter<String> itemAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lvItem = (ListView)this.findViewById(R.id.listview);
btnAdd = (Button)this.findViewById(R.id.btn);
itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,contactlist);
lvItem.setAdapter(itemAdapter);
btnAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
GetEmails();
Toast.makeText(Trial3.this, "working...", Toast.LENGTH_SHORT).show();
}
});
}
private void GetEmails()
{
ContentResolver cr =getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext())
{
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
Cursor emails = cr.query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + id, null, null);
while (emails.moveToNext())
{
emailAddress = emails.getString(emails.getColumnIndex(Email.DATA));
contactlist.add(emailAddress);
itemAdapter.notifyDataSetChanged();
}
emails.close();
}
cursor.close();
}
}
日志猫
06-14 15:03:17.339: D/AndroidRuntime(502): Shutting down VM
06-14 15:03:17.339: W/dalvikvm(502): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
06-14 15:03:17.339: E/AndroidRuntime(502): Uncaught handler: thread main exiting due to uncaught exception
06-14 15:03:17.349: E/AndroidRuntime(502): java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts from pid=502, uid=10028 requires android.permission.READ_CONTACTS
06-14 15:03:17.349: E/AndroidRuntime(502): at android.os.Parcel.readException(Parcel.java:1218)
06-14 15:03:17.349: E/AndroidRuntime(502): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160)
06-14 15:03:17.349: E/AndroidRuntime(502): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
06-14 15:03:17.349: E/AndroidRuntime(502): at android.content.ContentProviderProxy.bulkQuery(ContentProviderNative.java:326)
06-14 15:03:17.349: E/AndroidRuntime(502): at android.content.ContentProviderProxy.query(ContentProviderNative.java:345)
06-14 15:03:17.349: E/AndroidRuntime(502): at android.content.ContentResolver.query(ContentResolver.java:202)
06-14 15:03:17.349: E/AndroidRuntime(502): at com.abhi.Trial3.GetEmails(Trial3.java:45)
06-14 15:03:17.349: E/AndroidRuntime(502): at com.abhi.Trial3.access$0(Trial3.java:42)
06-14 15:03:17.349: E/AndroidRuntime(502): at com.abhi.Trial3$1.onClick(Trial3.java:36)
06-14 15:03:17.349: E/AndroidRuntime(502): at android.view.View.performClick(View.java:2364)
06-14 15:03:17.349: E/AndroidRuntime(502): at android.view.View.onTouchEvent(View.java:4179)
如何解决这个问题呢 ?