我在CommonsWare的书中关注如何使用Fragment处理异步任务的方向更改(使用setRetainInstance(true))。然后我尝试向异步任务添加一个进度对话框,但它继续在方向更改时崩溃,说“视图未附加到窗口”。
是否存在一个模式,用于在片段内部的asynctask内部使用进度对话框,该对话框不会丢失进度对话框或在方向更改时崩溃?
我是否仍然必须管理片段中进度对话框的生命周期,即使片段没有被销毁?我假设,因为在活动的UI上创建了进度对话框,我可能必须通过覆盖片段的onAttach()和onDetach()方法来销毁/重新创建进度对话框?但是,如果我使用片段,我认为Android应该负责重新附加活动。
答案 0 :(得分:0)
根据设计指南,ProgressDialog似乎已过时。看起来我们应该使用微调器或条形而不是文本。鉴于此信息,我认为正确的路线是使用微调器作为片段的布局,并在主要活动的布局中嵌入该布局。可以从片段管理显示/隐藏它的那种方式,从而处理方向改变。
不幸的是,我仍然收到以下错误。
08-27 17:38:07.647: E/AndroidRuntime(7863): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.app/com.my.app.ui.SmsVerificationActivity}: java.lang.ClassCastException: android.support.v4.app.NoSaveStateFrameLayout cannot be cast to android.widget.Button
08-27 17:38:07.647: E/AndroidRuntime(7863): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-27 17:38:07.647: E/AndroidRuntime(7863): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-27 17:38:07.647: E/AndroidRuntime(7863): at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-27 17:38:07.647: E/AndroidRuntime(7863): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-27 17:38:07.647: E/AndroidRuntime(7863): at android.os.Handler.dispatchMessage(Handler.java:99)
08-27 17:38:07.647: E/AndroidRuntime(7863): at android.os.Looper.loop(Looper.java:137)
08-27 17:38:07.647: E/AndroidRuntime(7863): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-27 17:38:07.647: E/AndroidRuntime(7863): at java.lang.reflect.Method.invokeNative(Native Method)
08-27 17:38:07.647: E/AndroidRuntime(7863): at java.lang.reflect.Method.invoke(Method.java:511)
08-27 17:38:07.647: E/AndroidRuntime(7863): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-27 17:38:07.647: E/AndroidRuntime(7863): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-27 17:38:07.647: E/AndroidRuntime(7863): at dalvik.system.NativeStart.main(Native Method)
08-27 17:38:07.647: E/AndroidRuntime(7863): Caused by: java.lang.ClassCastException: android.support.v4.app.NoSaveStateFrameLayout cannot be cast to android.widget.Button
08-27 17:38:07.647: E/AndroidRuntime(7863): at com.my.app.ui.SmsVerificationActivity.initResources(SmsVerificationActivity.java:52)
08-27 17:38:07.647: E/AndroidRuntime(7863): at com.my.app.ui.SmsVerificationActivity.onCreate(SmsVerificationActivity.java:29)
08-27 17:38:07.647: E/AndroidRuntime(7863): at android.app.Activity.performCreate(Activity.java:4465)
08-27 17:38:07.647: E/AndroidRuntime(7863): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-27 17:38:07.647: E/AndroidRuntime(7863): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
这是主要活动的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="60dip"
android:background="@drawable/gradient"
android:gravity="center_vertical"
android:paddingLeft="10dip"
android:text="@string/sms_verification_header"
android:textColor="#ffffff"
android:textSize="16dip" />
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/header" >
<TextView
android:id="@+id/verification_description_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/relative_layout"
android:background="#000000"
android:padding="10dip"
android:text="@string/sms_verification_step_one"
android:textColor="#ffffff"
android:textSize="16dip" />
<View
android:id="@+id/divider"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:layout_below="@+id/verification_description_text"
android:background="#808080" />
<EditText
android:id="@+id/phone_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/divider"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:imeOptions="flagNoExtractUi"
android:inputType="phone" />
</RelativeLayout>
<fragment
android:id="@+id/async_verification_fragment"
android:name="com.my.app.ui.AsyncVerificationFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_below="@+id/phone_number"
android:layout_weight="1" />
<LinearLayout
android:id="@id/connect_footer"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_alignParentBottom="true"
android:background="@drawable/gradient"
android:orientation="horizontal" >
<Button
android:id="@+id/verify"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:text="@string/yes" />
<Button
android:id="@+id/cancel"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:text="@string/cancel" />
</LinearLayout>
</RelativeLayout>
片段布局:
主要活动中的违规行代码:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sms_verification_activity);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
initResources();
}
private void initResources() {
TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
final String number = tm.getLine1Number();
mPhoneNumber = (EditText) findViewById(R.id.phone_number);
if (!TextUtils.isEmpty(number)) {
mPhoneNumber.setText(number);
}
final Button verify = (Button) findViewById(R.id.verify);
这是片段的onCreateView:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_progress_bar, container);
return v;
}