【Android项目闪退求助】安卓新手,请大神帮忙看看

Keyon同学 学生  2018-04-17 11:32:22
本人大二Android新手,在做课本例题时遇到了用SQLite时闪退的问题,请大佬给予指点

开发软件是Android Studio

代码如下:
public class MainActivity extends AppCompatActivity {

private String[] columns = { Contacts._ID, // 获得ID值
Contacts.DISPLAY_NAME,// 获得姓名
Phone.NUMBER,// 获得电话
Phone.CONTACT_ID };

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.result); // 获得布局文件中的文本框
tv.setText(getQueryData()); // 为文本框设置显示文本

}
private String getQueryData() {
StringBuilder sb = new StringBuilder(); // 用于保存字符串
ContentResolver resolver = getContentResolver(); // 获得ContentResolver对象
Cursor cursor = resolver.query(Contacts.CONTENT_URI, null, null, null, null);// 查询记录
while (cursor.moveToNext()) {
int idIndex = cursor.getColumnIndex(columns[0]); // 获得ID值的索引
int displayNameIndex = cursor.getColumnIndex(columns[1]); // 获得姓名索引
int id = cursor.getInt(idIndex); // 获得id
String displayName = cursor.getString(displayNameIndex); // 获得名称
Cursor phone = resolver.query(Phone.CONTENT_URI, null, columns[3] + "=" + id, null, null);
while (phone.moveToNext()) {
int phoneNumberIndex = phone.getColumnIndex(columns[2]); // 获得电话索引
String phoneNumber = phone.getString(phoneNumberIndex); // 获得电话
sb.append(displayName + ": " + phoneNumber + "\n"); // 保存数据
}
}
cursor.close();// 关闭游标
return sb.toString();
}

@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 boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}


log如下:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.exam8_8, PID: 21766
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.exam8_8/com.example.exam8_8.MainActivity}: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{f4cc77a 21766:com.example.exam8_8/u0a111} (pid=21766, uid=10111) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3027)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:101)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:73)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1786)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{f4cc77a 21766:com.example.exam8_8/u0a111} (pid=21766, uid=10111) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
at android.os.Parcel.createException(Parcel.java:1942)
at android.os.Parcel.readException(Parcel.java:1865)
at android.app.IActivityManager$Stub$Proxy.getContentProvider(IActivityManager.java:4967)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:5970)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2545)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1799)
at android.content.ContentResolver.query(ContentResolver.java:757)
at android.content.ContentResolver.query(ContentResolver.java:723)
at android.content.ContentResolver.query(ContentResolver.java:681)
at com.example.exam8_8.MainActivity.getQueryData(MainActivity.java:33)
at com.example.exam8_8.MainActivity.onCreate(MainActivity.java:27)
at android.app.Activity.performCreate(Activity.java:7117)
at android.app.Activity.performCreate(Activity.java:7108)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1262)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2867)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3027) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:101) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:73) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1786) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6656) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 
Caused by: android.os.RemoteException: Remote stack trace:
at com.android.server.am.ActivityManagerService.getContentProviderImpl(ActivityManagerService.java:11794)
at com.android.server.am.ActivityManagerService.getContentProvider(ActivityManagerService.java:12175)
at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:477)
at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:3114)
at android.os.Binder.execTransact(Binder.java:726)
Application terminated.
...全文
1336 点赞 收藏 11
写回复
11 条回复
kingiis 2018年04月19日
老夫传你一DBFlow ,从此数据库不用愁。
回复 点赞
懒笑翻 2018年04月18日
Caused by: java.lang.SecurityException: Caused by: android.os.RemoteException:
回复 点赞
x443137254 2018年04月17日
添加权限:android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
回复 点赞
键盘舞者113 2018年04月17日
把代码上传,贴出链接
回复 点赞
Android小码家 2018年04月17日
引用 4 楼 YK0307 的回复:
[quote=引用 3 楼 qq_20330595 的回复:] Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{f4cc77a 21766:com.example.exam8_8/u0a111} (pid=21766, uid=10111) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS 这个说的是需要获取 读取联系人权限 还有数据库 应该也需要 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
你好,我把这些代码已经加进Menifest里面了,但是log里还是提示同样的权限错误,然后我百度了下结果是因为Android 6.0往后更新了安全机制,需要用户授权,然后找到解决方法,添加如下代码来进行权限校验:
public class MainActivity extends Activity {
    final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
    int hasWriteContactsPermisson = checkSelfPermission(android.Manifest.permission.READ_CONTACTS);
    if(hasWriteContactsPermisson != PackageManager.PERMISSION_GRANTED)
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]
                            {Manifest.permission.WRITE_CONTACTS},
                    REQUEST_CODE_ASK_PERMISSIONS);
        }
        return;
    }
    //使用ContentResolver查找联系人数据
    Cursor cursor = getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI,
            null,null,null,null);
但是这里面的hasWriteContactsPermisson和PackageManager.PERMISSION_GRANTED报错Unknownclass,Alt+Enter也没有提示项,该怎么解决呢[/quote] 是不是这样? final private int REQUEST_CODE_ASK_PERMISSIONS = 123; @TargetApi(Build.VERSION_CODES.M) private void requeset() { int hasWriteContactsPermisson = checkSelfPermission(android.Manifest.permission.READ_CONTACTS); if (hasWriteContactsPermisson != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[] {Manifest.permission.WRITE_CONTACTS}, REQUEST_CODE_ASK_PERMISSIONS); } } @TargetApi(Build.VERSION_CODES.M) private void toRequeset() { int hasWriteContactsPermisson = ActivityCompat.checkSelfPermission(this,android.Manifest.permission.READ_CONTACTS); if (hasWriteContactsPermisson != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[] {Manifest.permission.WRITE_CONTACTS}, REQUEST_CODE_ASK_PERMISSIONS); } }
回复 点赞
Android小码家 2018年04月17日
引用 4 楼 YK0307 的回复:
[quote=引用 3 楼 qq_20330595 的回复:] Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{f4cc77a 21766:com.example.exam8_8/u0a111} (pid=21766, uid=10111) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS 这个说的是需要获取 读取联系人权限 还有数据库 应该也需要 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
你好,我把这些代码已经加进Menifest里面了,但是log里还是提示同样的权限错误,然后我百度了下结果是因为Android 6.0往后更新了安全机制,需要用户授权,然后找到解决方法,添加如下代码来进行权限校验:
public class MainActivity extends Activity {
    final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
    int hasWriteContactsPermisson = checkSelfPermission(android.Manifest.permission.READ_CONTACTS);
    if(hasWriteContactsPermisson != PackageManager.PERMISSION_GRANTED)
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]
                            {Manifest.permission.WRITE_CONTACTS},
                    REQUEST_CODE_ASK_PERMISSIONS);
        }
        return;
    }
    //使用ContentResolver查找联系人数据
    Cursor cursor = getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI,
            null,null,null,null);
但是这里面的hasWriteContactsPermisson和PackageManager.PERMISSION_GRANTED报错Unknownclass,Alt+Enter也没有提示项,该怎么解决呢[/quote] 用ActivityCompat这个类。或者前面我写的 private void toRequeset() { int hasWriteContactsPermisson = ActivityCompat.checkSelfPermission(this,android.Manifest.permission.READ_CONTACTS); if (hasWriteContactsPermisson != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this,new String[] {Manifest.permission.WRITE_CONTACTS}, REQUEST_CODE_ASK_PERMISSIONS); } }
回复 点赞
qq_31461209 2018年04月17日
引用 5 楼 qq_31461209 的回复:
String[] permission = new String[]{Manifest.permission.READ_CONTACTS}; if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions(this,permission,1); }
这个是安卓6.0以上动态获取权限的
回复 点赞
qq_31461209 2018年04月17日
楼主你试下这样写 应该是没有问题的
回复 点赞
qq_31461209 2018年04月17日
String[] permission = new String[]{Manifest.permission.READ_CONTACTS}; if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions(this,permission,1); }
回复 点赞
Keyon同学 2018年04月17日
引用 3 楼 qq_20330595 的回复:
Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{f4cc77a 21766:com.example.exam8_8/u0a111} (pid=21766, uid=10111) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS 这个说的是需要获取 读取联系人权限 还有数据库 应该也需要 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
你好,我把这些代码已经加进Menifest里面了,但是log里还是提示同样的权限错误,然后我百度了下结果是因为Android 6.0往后更新了安全机制,需要用户授权,然后找到解决方法,添加如下代码来进行权限校验:
public class MainActivity extends Activity {
    final private int REQUEST_CODE_ASK_PERMISSIONS = 123;
    int hasWriteContactsPermisson = checkSelfPermission(android.Manifest.permission.READ_CONTACTS);
    if(hasWriteContactsPermisson != PackageManager.PERMISSION_GRANTED)
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]
                            {Manifest.permission.WRITE_CONTACTS},
                    REQUEST_CODE_ASK_PERMISSIONS);
        }
        return;
    }
    //使用ContentResolver查找联系人数据
    Cursor cursor = getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI,
            null,null,null,null);
但是这里面的hasWriteContactsPermisson和PackageManager.PERMISSION_GRANTED报错Unknownclass,Alt+Enter也没有提示项,该怎么解决呢
回复 点赞
Android小码家 2018年04月17日
Caused by: java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2 from ProcessRecord{f4cc77a 21766:com.example.exam8_8/u0a111} (pid=21766, uid=10111) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS 这个说的是需要获取 读取联系人权限 还有数据库 应该也需要 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
回复 点赞
发动态
发帖子
Android
创建于2009-10-09

4.6w+

社区成员

9.0w+

社区内容

移动平台 Android
社区公告
暂无公告