2012-07-03 55 views
-6

好了,所以我在寫代碼和它工作得很好,直到我得到這個問題:語法錯誤,標識預計

令牌「捕捉」,標識預計

語法錯誤

這是有問題的代碼:

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

/me facepalms ... okie ...你有什麼**嘗試**'{'d'要做什麼? – t0mm13b

回答

5

你的錯誤是因爲你沒有catchtry。我建議您在嘗試解決Android項目之前先熟悉一下Java語法。

+1

好吧,所以你說我應該放棄我的應用程序,直到我更好地瞭解java?因爲這沒有發生。 – user1287195

+1

不,我建議你在嘗試一些如此複雜的東西之前,更好地掌握java語法。我沒有說過放棄任何事情。只要做一些事情就可以了......它就像在跑步前學習走路一樣。如果你在網上瀏覽免費的Java課程,你會發現一些不會花費太多時間,並且你將有更好的機會成功地做出你想做的任何事情。 – FoamyGuy

+0

噢,好吧,這是有道理的 – user1287195

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); 
    } 
} 
相關問題