admin管理员组

文章数量:1391947

I am a beginner of android development, and I encounter some problem when I try to do the exercise of using other system application like phone and message. There is my code.

 @Override
    public void onClick(View view) {

        String phoneNo = "12345";
        Intent intent = new Intent();
        if(view.getId()==R.id.btn_dial){

            intent.setAction(Intent.ACTION_DIAL);
            Uri uri = Uri.parse("tel:"+phoneNo);
            intent.setData(uri);
            startActivity(intent);
        }
        if(view.getId()==R.id.btn_sms){
            intent.setAction(Intent.ACTION_SENDTO);
            Uri uri2 = Uri.parse("smsto"+phoneNo);
            intent.setData(uri2);
            startActivity(intent);
        }
        if(view.getId()==R.id.btn_myact){
            intent.setAction("android.intent.action.KING");
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            startActivity(intent);
        }

    }

There are two problems: (1) At the beginning, I want to use switch(view.getId())to handle the branch, but Android Studio give me a error of Constant expression required when I use:

case R.id.btn_dial:

I would like to know why?

(2) The program can be successfully executed but when I click the btn of go to message, the application will crash. But it work when I click phone btn and myact btn.

I would like to konw how to tackle these problems.

Finally, I expected that I want to konw the reason why I cannot use switch and can turn to message application when I click the msg btn.

I am a beginner of android development, and I encounter some problem when I try to do the exercise of using other system application like phone and message. There is my code.

 @Override
    public void onClick(View view) {

        String phoneNo = "12345";
        Intent intent = new Intent();
        if(view.getId()==R.id.btn_dial){

            intent.setAction(Intent.ACTION_DIAL);
            Uri uri = Uri.parse("tel:"+phoneNo);
            intent.setData(uri);
            startActivity(intent);
        }
        if(view.getId()==R.id.btn_sms){
            intent.setAction(Intent.ACTION_SENDTO);
            Uri uri2 = Uri.parse("smsto"+phoneNo);
            intent.setData(uri2);
            startActivity(intent);
        }
        if(view.getId()==R.id.btn_myact){
            intent.setAction("android.intent.action.KING");
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            startActivity(intent);
        }

    }

There are two problems: (1) At the beginning, I want to use switch(view.getId())to handle the branch, but Android Studio give me a error of Constant expression required when I use:

case R.id.btn_dial:

I would like to know why?

(2) The program can be successfully executed but when I click the btn of go to message, the application will crash. But it work when I click phone btn and myact btn.

I would like to konw how to tackle these problems.

Finally, I expected that I want to konw the reason why I cannot use switch and can turn to message application when I click the msg btn.

Share Improve this question edited Mar 13 at 11:04 King Ng asked Mar 13 at 11:02 King NgKing Ng 11 bronze badge 4
  • because switch expects a 'constant ', not a "result of a method call". try : int i = view.getId(); switch(i) – Stultuske Commented Mar 13 at 11:11
  • @Stultuske sorry that I have tried it but still not work. – King Ng Commented Mar 13 at 12:10
  • King Ng sorry, I was mistaken: it's in the 'case' block that it has to be a constant – Stultuske Commented Mar 13 at 12:29
  • If you really want to use a switch statement you can disable "nonFinalResIds" discussed here. – Computable Commented Mar 13 at 21:45
Add a comment  | 

1 Answer 1

Reset to default 1
@Override
public void onClick(View view) {
    String phoneNo = "12345";
    Intent intent = new Intent();

    int id = view.getId();  // Get the view ID

    if (id == R.id.btn_dial) {
        intent.setAction(Intent.ACTION_DIAL);
        Uri uri = Uri.parse("tel:" + phoneNo);
        intent.setData(uri);
        startActivity(intent);
    } else if (id == R.id.btn_sms) {
        intent.setAction(Intent.ACTION_SENDTO);
        Uri uri2 = Uri.parse("smsto:" + phoneNo);
        intent.setData(uri2);
        startActivity(intent);
    } else if (id == R.id.btn_myact) {
        intent.setAction("android.intent.action.KING");
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        startActivity(intent);
    }
}

you can change "smsto" replace with "smsto:"

本文标签: javaWhy the app will crush when I turn to message and cannot use switchStack Overflow