onCreateDialog方法中的异常?

时间:2011-09-20 07:19:50

标签: android

我正在

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
2.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);


}

5 个答案:

答案 0 :(得分:2)

试试这个:

return alert.create();

而不是:

alert.show(); 

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

答案 1 :(得分:1)

protected Dialog onCreateDialog (int id, Bundle args)
  

不推荐使用此方法。使用新的DialogFragment类   而是FragmentManager;这也适用于旧平台   通过Android兼容包。回调创建   活动管理(保存和恢复)的对话框。   默认实现调用onCreateDialog(int)   兼容性。如果您的目标是HONEYCOMB或更高版本,请考虑   而是使用DialogFragment代替。

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

答案 2 :(得分: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();

答案 3 :(得分:0)

尝试alert.create().show();

报告,如果它不起作用。

答案 4 :(得分: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) 
    {

    }
}