当我单击应用程序时,不幸的是,应用程序已停止。
我不知道如何在 Android Studio 中解决这个错误,请帮我解决这个问题
这是显示收件箱短信的代码
public class MainActivity extends AppCompatActivity {
ListView lv;
Cursor cr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dip();
}
public void dip(){
lv=(ListView)findViewById(R.id.ls);
ArrayList<String> s;
s = readAllData();
String ms="";
for (int i=0;i<s.size();i++) {
ms+=s.get(i)+",";
}
String messageArray[];
messageArray=ms.split(",");
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,android.R.id.text1,messageArray);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
public ArrayList<String> readAllData() {
ArrayList<String> sms = new ArrayList<>();
Uri us = Uri.parse("content://sms/inbox");
cr = getContentResolver().query(us, null, null, null, null);
assert cr != null;
cr.moveToFirst();
while (cr.moveToNext()) {
String body = cr.getString(cr.getColumnIndexOrThrow("body"));
sms.add(body);
}
return sms;
}
这是 LogCat 异常
D/AndroidRuntime: Shutting down VM
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: Process: example.prathik.com.myapplication, PID: 12003
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{example.prathik.com.myapplication/example.prathik.com.myapplication.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.telephony.SmsProvider uri content://sms/inbox from pid=12003, uid=10058 requires android.permission.READ_SMS, or grantUriPermission()
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.telephony.SmsProvider uri content://sms/inbox from pid=12003, uid=10058 requires android.permission.READ_SMS, or grantUriPermission()
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at android.os.Parcel.readException(Parcel.java:1599)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at android.content.ContentResolver.query(ContentResolver.java:491)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at android.content.ContentResolver.query(ContentResolver.java:434)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at example.prathik.com.myapplication.MainActivity.readAllData(MainActivity.java:51)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at example.prathik.com.myapplication.MainActivity.dip(MainActivity.java:32)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at example.prathik.com.myapplication.MainActivity.onCreate(MainActivity.java:26)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at android.app.Activity.performCreate(Activity.java:6237)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at android.app.ActivityThread.-wrap11(ActivityThread.java)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at android.os.Looper.loop(Looper.java:148)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5417)
12-29 12:24:03.876 12003-12003/example.prathik.com.myapplication E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method