2011-09-20 34 views
2

我在2.1和更低的版本越來越onCreateDialog方法中的異常?

09-20 12:42:26.697: ERROR/AndroidRuntime(721): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dashboardnew/com.dashboardnew.GmailFetchActivity}: java.lang.IllegalArgumentException: Activity#onCreateDialog did not create a dialog for id 1 

例外。

@Override 
protected Dialog onCreateDialog(int id) { 

    LayoutInflater factory = LayoutInflater.from(this); 

final View textEntryView = factory.inflate(R.layout.alertdialog_gmail, null); 

AlertDialog.Builder alert = new AlertDialog.Builder(this); 

alert.setTitle("Gmail login"); 
alert.setMessage("Enter your username and password"); 
// Set an EditText view to get user input 
alert.setView(textEntryView);  

final EditText username = (EditText) textEntryView.findViewById(R.id.edit_username); 
final EditText password = (EditText) textEntryView.findViewById(R.id.edit_password); 

alert.setPositiveButton("Login", new DialogInterface.OnClickListener() { 
public void onClick(DialogInterface dialog, int whichButton) { 

    String user=username.getText().toString(); 
    String pass=password.getText().toString(); 

    new FetchGmail().execute(user+"@gmail.com",pass); 
    progressDialog2.show(); 


} 
}); 

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton) { 
    // Canceled. 
    } 
}); 

alert.show(); 


    // TODO Auto-generated method stub 
    return super.onCreateDialog(id); 


} 

回答

2

試試這個:

return alert.create(); 

代替:

alert.show(); 

// TODO Auto-generated method stub 
return super.onCreateDialog(id); 
+0

它的工作thks !! cnüPLZ告訴我什麼是問題 – Harinder

+1

我不是很確定爲什麼super.onCreateDialog(ID)在這裏不工作,但在您的代碼警報是一個AlertDialog.Builder實例得到一個你需要調用create()的對話框,如果你不需要調用super方法提供您自己的對話 – cwin

0

可能是這樣的代碼可以幫助你......

Builder pinBuilder = new AlertDialog.Builder(context); 
    pinBuilder.setView(pinHolder).setTitle("Title") 
     .setMessage("Set Message") 
     .setPositiveButton("Ok", new DialogInterface.OnClickListener() 
     { 
      @Override 
      public void onClick(DialogInterface dialog, int which) 
      { 



      } 
    }). 
    setNegativeButton("Cancel",new DialogInterface.OnClickListener() 
    {   
      @Override 
      public void onClick(DialogInterface dialog,int which) 
      { 

      } 
    }).show(); 
+0

沒有工作!同樣的問題 – Harinder

+0

不要把這個代碼在onCreateDialog()method.Put此代碼在正常活動 –

1
protected Dialog onCreateDialog (int id, Bundle args) 

此方法已棄用。改爲使用帶有 FragmentManager的新DialogFragment類;這也可以通過Android兼容性軟件包在舊版平臺 上獲得。用於創建 對話框的回調,該對話框由活動爲您進行管理(保存和恢復)。 對於 兼容性,默認實現會調用onCreateDialog(int)。如果您的目標是HONEYCOMB或更高版本,請改爲使用DialogFragment代替 。

http://developer.android.com/reference/android/app/Activity.html#onCreateDialog%28int,%20android.os.Bundle%29

0

嘗試alert.create().show();

報告回來,如果它不工作。

+0

我應該返回什麼?它解決了檢查接受的答案,你可以解釋什麼是錯的 – Harinder

0

可能是這段代碼可能對你有幫助。 首先創建擴展對話框類,然後從你的活動調用此

CustomizeDilogForAddPhoto customizeDialog = new CustomizeDilogForAddPhoto(conntext,activity); 
       customizeDialog.show(); 

這是你的對話框類

public class CustomizeDilogForAddPhoto extends Dialog implements OnClickListener 
{ 
    public CustomizeDilogForAddPhoto(Context context,Activity activity) 
    { 
     super(context); 

     this.context=context; 
     this.activity=activity; 
     this.mainActivityname=maintabactivity; 

     requestWindowFeature(Window.FEATURE_NO_TITLE); 

     requestWindowFeature(android.R.style.Theme_Translucent_NoTitleBar); 
     /** Design the dialog in main.xml file */ 
     setContentView(R.layout.custume_add_photo_dilog); 

     btnClose.setOnClickListener(this); 
     btnGallary.setOnClickListener(this); 

    } 
    @Override 
    public void onClick(View v) 
    { 

    } 
}