2011-04-05 44 views
0

How to center progress indicator in ProgressDialog easily (when no title/text passed along)Android調用自定義ProgressDialog - 如何?

試圖創建一個自定義progressdialog,但沒有工作..

我的代碼:

public class CustomProgressDialog extends ProgressDialog { 

public CustomProgressDialog(Context context) { 
     super(context, R.style.progress_dialog); 
} 

public static CustomProgressDialog show(Context context, CharSequence title, 
     CharSequence message) { 
    return show(context, title, message, false); 
} 

public static CustomProgressDialog show(Context context, CharSequence title, 
     CharSequence message, boolean indeterminate) { 
    return show(context, title, message, indeterminate, false, null); 
} 

public static CustomProgressDialog show(Context context, CharSequence title, 
     CharSequence message, boolean indeterminate, boolean cancelable) { 
    return show(context, title, message, indeterminate, cancelable, null); 
} 

public static CustomProgressDialog show(Context context, CharSequence title, 
     CharSequence message, boolean indeterminate, 
     boolean cancelable, OnCancelListener cancelListener) { 
    CustomProgressDialog dialog = new CustomProgressDialog(context); 
    dialog.setTitle(title); 
    dialog.setCancelable(cancelable); 
    dialog.setOnCancelListener(cancelListener); 
    dialog.addContentView(new ProgressBar(context), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    dialog.show(); 

    return dialog; 
} 
} 

以及如何ID喜歡稱:

public static CustomProgressDialog dialog; 

public static void showLoaderDialog(String sHead, String sMess) { 
    dialog=new CustomProgressDialog(mycontext).show(mycontext, sHead, sMess, true, true); 
} 
public static void hideLoaderDialog() { 
    dialog.dismiss(); 
} 

,但我得到只有崩潰:(爲什麼?怎麼能調用這個自定義對話框?

錯誤日誌:

04-05 20:33:12.754: ERROR/AndroidRuntime(13165): android.util.AndroidRuntimeException: requestFeature() must be called before adding content 
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):  at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:174) 
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):  at com.android.internal.app.AlertController.installContent(AlertController.java:201) 
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):  at android.app.AlertDialog.onCreate(AlertDialog.java:260) 
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):  at android.app.ProgressDialog.onCreate(ProgressDialog.java:176) 
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):  at android.app.Dialog.dispatchOnCreate(Dialog.java:307) 
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):  at android.app.Dialog.show(Dialog.java:225) 
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):  at com.lacas.chathuProto.CustomProgressDialog.show(CustomProgressDialog.java:37) 
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):  at com.lacas.chathuProto.CustomProgressDialog.show(CustomProgressDialog.java:26) 
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):  at com.lacas.chathuProto.chathuStart.showLoaderDialog(chathuStart.java:814) 
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):  at com.lacas.chathuProto.chathuPROTO$12.run(chathuPROTO.java:1159) 
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):  at android.os.Handler.handleCallback(Handler.java:587) 
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):  at android.os.Handler.dispatchMessage(Handler.java:92) 
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):  at android.os.Looper.loop(Looper.java:123) 
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):  at java.lang.reflect.Method.invoke(Method.java:521) 
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
04-05 20:33:12.754: ERROR/AndroidRuntime(13165):  at dalvik.system.NativeStart.main(Native Method) 
+0

顯示,請,錯誤日誌。 – 2011-04-05 18:10:09

+0

現在編輯...... – lacas 2011-04-05 18:35:14

+0

在原始文章中,Macarse從'Dialog'擴展了自定義對話框,而不是'ProgressDialog'。也許這就是問題所在? – bigstones 2011-04-05 19:23:20

回答

1

也許,你應該叫對話框中這樣說:

public static void showLoaderDialog(String sHead, String sMess) { 
    dialog=new CustomProgressDialog(mycontext); 
    dialog.show(mycontext, sHead, sMess, true, true); 
} 

但我可能是錯的。

無論如何,顯示,請稍等代碼行是指CustomProgressDialog.java:37