eclipse 标记“catch”的语法错误,需要标识符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11302914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 18:27:30  来源:igfitidea点击:

Syntax error on token "catch", Identifier expected

androideclipse

提问by user1287195

Okay, so I was writing code and it was working just fine until I got to this problem:

好的,所以我正在编写代码并且它工作得很好,直到我遇到这个问题:

Syntax error on token "catch", Identifier expected

标记“catch”的语法错误,需要标识符

Here is the code with the problem:

这是有问题的代码:

public void onClick(View arg0) {

    EditText num=(EditText)findViewById(R.id.editText1);
    String number = "tel:" +num.getText().toString().trim();
    Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(number));
    startActivity(callIntent);

    TelephonyManager tManager = (TelephonyManager)                      
    getSystemService(Context.TELEPHONY_SERVICE);
    listener = new ListenToPhoneState();
    tManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

    //here's the problem 
    } catch (ActivityNotFoundException activityException) {
        Log.e("telephony-example", "Call failed", activityException);
    }


    private class ListenToPhoneState extends PhoneStateListener {

        public void onCallStateChanged(int state, String incomingNumber) {
            Log.i("telephony-example", "State changed: " + stateName(state));
        }

        String stateName(int state) {
        switch (state) {
            case TelephonyManager.CALL_STATE_IDLE: return "Idle";
            case TelephonyManager.CALL_STATE_OFFHOOK: return "Off hook";
            case TelephonyManager.CALL_STATE_RINGING: return "Ringing";
        }
        return Integer.toString(state);
    }
}

回答by FoamyGuy

Your error is because you have catchwithout try. I suggest you familiarize yourself with java syntax a little bit better before you attempt to tackle an Android project.

你的错误是因为你catch没有try. 我建议您在尝试处理 Android 项目之前先熟悉一下 java 语法。

回答by Lion

Put a tryblock as shown below.

放置一个try块,如下所示。

public void onClick(View arg0) 
{
    try
    {
         EditText num=(EditText)findViewById(R.id.editText1);
         String number = "tel:" +num.getText().toString().trim();
         Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse(number));
         startActivity(callIntent);

         TelephonyManager tManager = (TelephonyManager)
         getSystemService(Context.TELEPHONY_SERVICE);
         listener = new ListenToPhoneState();
         tManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
    } 
    catch (ActivityNotFoundException activityException) 
    {
        Log.e("telephony-example", "Call failed", activityException);
    }
}