2015-07-01 148 views
-1

請告訴我我的代碼有什麼問題? .setPositiveButton方法拋出了紅線錯誤。它無法讀取smsText。我試圖宣佈smsText公開,但問題是一樣的。無法解決方法錯誤

public class PrepaidSMSBase extends Activity { 

private String smsNumber = ""; 
private String smsPackageName; 
private String smsPrice; 
private String smsTitle; 
private String smsText; 


public PrepaidSMSBase(){} 

public void setSmsPackageInformation(String smsTitle, String smsPackageName, String smsPrice, String smsNumber, String smsText) 
{ 
    this.smsTitle = smsTitle; 
    this.smsPackageName = smsPackageName; 
    this.smsPrice = smsPrice; 
    this.smsNumber = smsNumber; 
    this.smsText = smsText; 

} 

public void activeSMSPackage() 
{ 
    try 
    { 
     AlertDialog.Builder builder = new AlertDialog.Builder(PrepaidSMSBase.this); 
     builder.setTitle(smsTitle); 
     builder.setMessage("Are you sure you want to active" + PrepaidSMSBase.this.smsPackageName + "in RS: " + 
       PrepaidSMSBase.this.smsPrice); 
     builder.setPositiveButton(new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       Uri uri = Uri.parse("smsto:" + smsNumber); 
       Intent intent = new Intent(Intent.ACTION_SENDTO, uri); 
       intent.putExtra("sms_body", smsText); 
       startActivity(intent); 
       Toast.makeText(getApplicationContext(), "Please click send button to activate desire Package", Toast.LENGTH_LONG).show(); 
      } 
     }); 
    }catch (ActivityNotFoundException e){ 
     e.printStackTrace(); 
    } 
} 

}

+0

進口所需的所有類。 OnClickListener()應該是父級DialogInterface。還導入DialogInterface – EagleEye

+0

問題仍然exixt –

+0

什麼是「reline」的錯誤描述? – EagleEye

回答

0

沒有setPositiveButton只有一個參數。正如你可以在文檔中看到有兩種方法setPositiveButton都是具有兩個參數:

public AlertDialog.Builder setPositiveButton (int textId, DialogInterface.OnClickListener listener)

public AlertDialog.Builder setPositiveButton (CharSequence text, DialogInterface.OnClickListener listener)

改變這樣的代碼:

AlertDialog.Builder builder = new AlertDialog.Builder(PrepaidSMSBase.this); 
builder.setTitle(smsTitle); 
builder.setMessage("Are you sure you want to active" + PrepaidSMSBase.this.smsPackageName + "in RS: " + 
     PrepaidSMSBase.this.smsPrice); 
builder.setPositiveButton("Button text", new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     Uri uri = Uri.parse("smsto:" + smsNumber); 
     Intent intent = new Intent(Intent.ACTION_SENDTO, uri); 
     intent.putExtra("sms_body", smsText); 
     startActivity(intent); 
     Toast.makeText(getApplicationContext(), "Please click send button to activate desire Package", Toast.LENGTH_LONG).show(); 
    } 
});