调用onClick方法时,应用崩溃

时间:2019-07-05 13:06:24

标签: android permissions

我想从手机中读取短信,但应用程序崩溃。

public class MainActivity extends AppCompatActivity {

    TextView tx ;
    public static final int MY_PERMISSIONS_REQUEST_CODE = 1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tx = findViewById(R.id.text);


    }
    public void grant(View view) {
        if(ContextCompat.checkSelfPermission(this,Manifest.permission.READ_SMS ) == PackageManager.PERMISSION_GRANTED)
        {
            Toast.makeText(this, "Permission is granted", Toast.LENGTH_SHORT).show();
        }
        else
        {
                ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_SMS},MY_PERMISSIONS_REQUEST_CODE);
        }
    }

    public void show(View view)
    {


        Uri uri = Uri.parse("content://sms/inbox");
        final Cursor cur = getContentResolver().query(uri,
                null, null, null, null);
        String sms = "";
        while (cur.moveToNext()) {
            sms += "From :" + cur.getString(1) + " : " + cur.getString(3)+"\n";
        }
        tx.setText(sms);
        setContentView(view);
    }

错误消息:

Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
        at android.view.View.performClick(View.java:7339) 
        at android.widget.TextView.performClick(TextView.java:14221) 
        at android.view.View.performClickInternal(View.java:7305) 
        at android.view.View.access$3200(View.java:846) 
        at android.view.View$PerformClick.run(View.java:27787) 
        at android.os.Handler.handleCallback(Handler.java:873) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7058) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965) 
     Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
        at android.view.ViewGroup.addViewInner(ViewGroup.java:5259)
        at android.view.ViewGroup.addView(ViewGroup.java:5090)
        at android.view.ViewGroup.addView(ViewGroup.java:5030)
        at android.view.ViewGroup.addView(ViewGroup.java:5003)
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:460)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:145)
        at com.example.final_project.MainActivity.show(MainActivity.java:76)

1 个答案:

答案 0 :(得分:1)

摆脱第二个setContentView()

    tx.setText(sms);
    setContentView(view);  // <<- here
}

setContentView()仅应被用来设置活动的布局,该布局已在onCreate()中设置。因此,传递到View的{​​{1}}对象已经具有一个父对象,因为它已经被夸大,因此,如果不与它的原始父对象分离,就无法重用。我假设如果此方法充当show(View)界面,则单击View