令牌“catch”上的语法错误,预期的标识符

时间:2012-07-03 00:17:04

标签: android eclipse

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

令牌“catch”上的语法错误,预期标识符

以下是有问题的代码:

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);
    }
}

2 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:2)

如下所示放置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);
    }
}