package com.example.smsread;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView listView;
private static final int PERMISSIONS_REQUEST_READ_CONTACTS = 100;
ArrayList<String> smsList;
//private static final String TAG == MainActivity.class.getSimpleName()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.idList);
int permissionCheck = ContextCompat.checkSelfPermission( this,
Manifest.permission.READ_SMS);
if(permissionCheck == PackageManager.PERMISSION_GRANTED) {
showContacts();
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_SMS},
PERMISSIONS_REQUEST_READ_CONTACTS);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResult){
if(requestCode == PERMISSIONS_REQUEST_READ_CONTACTS){
if(grantResult[0] == PackageManager.PERMISSION_GRANTED) {
showContacts();
}else {
Toast.makeText(this,
"Until you grant the permission, we cannot display the names",
Toast.LENGTH_SHORT).show();
}
}
}
private void showContacts(){
Uri inboxUri = Uri.parse("content://sms/inbox");
smsList = new ArrayList<>();
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(inboxUri, null, null, null, null);
while (cursor.moveToNext()){
String number = cursor.getString(cursor.getColumnIndexOrThrow("address")).toString();
String body = cursor.getString(cursor.getColumnIndexOrThrow("body")).toString();
//String number = "098765432";
//String body = "lat: 33.584317N long: 73.045458";
String data = body;
String[] args = data.split(" ");
// String a = body.trim();
// String latitude = args[1];
// String longitude = args[1];
// Log.d("Args",a);
Log.d("Args",args[1]);
Log.d("Args",args[3]);
smsList.add("Number:" + number + "\n" + "Body:" + body + "\n"+"latitude:" +args[1]+ "\n" + "longitude:" +args[3] );
//+ "\n" + "latitude:" +args[1]+ "\n" + "longitude:" +args[3]
}
cursor.close();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
smsList);
listView.setAdapter(adapter);
}
}
错误:E / Android运行时:致命异常:主要 流程:com.example.smsread,PID:28273 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.smsread / com.example.smsread.MainActivity}: java.lang.ArrayIndexOutOfBoundsException:length = 1;索引= 1 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3430) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614) 在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86) 在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2199) 在android.os.Handler.dispatchMessage(Handler.java:112) 在android.os.Looper.loop(Looper.java:216) 在android.app.ActivityThread.main(ActivityThread.java:7625) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:524) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987) 由以下原因引起:java.lang.ArrayIndexOutOfBoundsException:length = 1;索引= 1 在com.example.smsread.MainActivity.showContacts(MainActivity.java:83) 在com.example.smsread.MainActivity.onCreate(MainActivity.java:44) 在android.app.Activity.performCreate(Activity.java:7458) 在android.app.Activity.performCreate(Activity.java:7448) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1286) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3409) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3614) 在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:86) 在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2199) 在android.os.Handler.dispatchMessage(Handler.java:112) 在android.os.Looper.loop(Looper.java:216) 在android.app.ActivityThread.main(ActivityThread.java:7625) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:524) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987) I / Process:正在发送信号。 PID:28273 SIG:9进程28273已终止。
为什么会出现此错误?
答案 0 :(得分:1)
由于ArrayIndexOutOfBoundsException。将数据拆分为args []并尝试记录args [1]和args [3]时,请确保它具有足够的元素。最好使用更多的安全构造,例如:
i = 0
for p in ax.patches:
ax.annotate("%.4f" % albion_dataset["Average Speeds"][i], (p.get_x() + p.get_width(), p.get_y() + 1.2),
xytext=(5, 10), textcoords='offset points')
print(i)
i += 1