-6

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

令牌“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);
    }
}
4

2 回答 2

5

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

于 2012-07-03T00:18:27.210 回答
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);
    }
}
于 2012-07-03T00:24:45.820 回答