मैं एंड्रॉइड को अभिविन्यास परिवर्तन को संभालने देना चाहता हूं। दुर्भाग्य से जब भी एक संवाद खुला होता है और अभिविन्यास परिवर्तन होता है तो यह क्रैश हो जाता है। मैं पहले से ही कारण संदेह है, लेकिन यह कैसे संभाल करने के लिए पता नहीं है:ओरिएंटेशन चेंज: IllegalStateException जब डायलॉगफ्रेगमेंट दिखाई देता है
आवेदन पहले backstack शुरू होता है इस तरह दिखता है:
02-07 15:26:03.316: I/System.out(14541): Added Fragments:
02-07 15:26:03.316: I/System.out(14541): #0: PortraitModeFragment{41963330 #0 id=0x7f090001 ListViewFragment}
02-07 15:26:03.326: I/System.out(14541): #1: MyDialogFragment{4199f6a0 #1 MyDialogFragment}
:
02-07 15:25:36.517: I/System.out(14541): Added Fragments:
02-07 15:25:36.517: I/System.out(14541): #0: PortraitModeFragment{41963330 #0 id=0x7f090001 ListViewFragment}
महान, अब हम एक संवाद खोलने
02-07 15:26:37.502: E/AndroidRuntime(14541): FATAL EXCEPTION: main
02-07 15:26:37.502: E/AndroidRuntime(14541): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testapp/com.example.testapp.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
02-07 15:26:37.502: E/AndroidRuntime(14541): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
02-07 15:26:37.502: E/AndroidRuntime(14541): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-07 15:26:37.502: E/AndroidRuntime(14541): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3692)
02-07 15:26:37.502: E/AndroidRuntime(14541): at android.app.ActivityThread.access$700(ActivityThread.java:141)
02-07 15:26:37.502: E/AndroidRuntime(14541): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1240)
02-07 15:26:37.502: E/AndroidRuntime(14541): at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 15:26:37.502: E/AndroidRuntime(14541): at android.os.Looper.loop(Looper.java:137)
02-07 15:26:37.502: E/AndroidRuntime(14541): at android.app.ActivityThread.main(ActivityThread.java:5039)
02-07 15:26:37.502: E/AndroidRuntime(14541): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 15:26:37.502: E/AndroidRuntime(14541): at java.lang.reflect.Method.invoke(Method.java:511)
02-07 15:26:37.502: E/AndroidRuntime(14541): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-07 15:26:37.502: E/AndroidRuntime(14541): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-07 15:26:37.502: E/AndroidRuntime(14541): at dalvik.system.NativeStart.main(Native Method)
02-07 15:26:37.502: E/AndroidRuntime(14541): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
02-07 15:26:37.502: E/AndroidRuntime(14541): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
02-07 15:26:37.502: E/AndroidRuntime(14541): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
02-07 15:26:37.502: E/AndroidRuntime(14541): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
02-07 15:26:37.502: E/AndroidRuntime(14541): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
02-07 15:26:37.502: E/AndroidRuntime(14541): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
02-07 15:26:37.502: E/AndroidRuntime(14541): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
02-07 15:26:37.502: E/AndroidRuntime(14541): at android.app.Activity.setContentView(Activity.java:1881)
02-07 15:26:37.502: E/AndroidRuntime(14541): at com.example.testapp.MainActivity.onCreate(MainActivity.java:17)
02-07 15:26:37.502: E/AndroidRuntime(14541): at android.app.Activity.performCreate(Activity.java:5104)
02-07 15:26:37.502: E/AndroidRuntime(14541): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
02-07 15:26:37.502: E/AndroidRuntime(14541): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
02-07 15:26:37.502: E/AndroidRuntime(14541): ... 12 more
02-07 15:26:37.502: E/AndroidRuntime(14541): Caused by: java.lang.IllegalStateException: Fragment com.example.testapp.fragments.LandscapeModeFragment did not create a view.
02-07 15:26:37.502: E/AndroidRuntime(14541): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:303)
02-07 15:26:37.502: E/AndroidRuntime(14541): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
दिलचस्प मजेदार तथ्य:
और अब जब डिवाइस बारी बारी से यह ऐसा नहीं होता है, तो मैं पहली बार, डिवाइस बिलकुल पलट गया और बाद में संवाद खोलने और ओरिएंटेशन बदलने क्योंकि दोनों टुकड़े पहले से लोड कर रहे हैं और शांति से backstack में मौजूद है:
02-07 15:34:00.015: I/System.out(15412): #0: PortraitModeragment{419b99b0 #0 id=0x7f090001 ListViewFragment}
02-07 15:34:00.015: I/System.out(15412): #1: LandscapeModeFragment{419b9b88 #1 id=0x7f090003 LandscapeModeFragment}
02-07 15:34:00.025: I/System.out(15412): #2: MyDialogFragment{419d1da0 #2 MyDialogFragment}
मैं नहीं कर सकते उपयोगकर्ता बनाने के उपयोग करने से पहले चारों ओर स्मार्टफोन घुमा मेरी ऐप, तो क्या करना है? ;)
पुन: पेश करने के लिए:
मैं संवाद कैसे जोड़ backstack रहे हैं:
1:
MyDialogFragment myTvShowDialog = new MyDialogFragment();
myTvShowDialog.show(getActivity().getSupportFragmentManager(),myTvShowDialog.TAG);
क्या मैंने किया MainActivity के लिए दो XML फ़ाइलें बना था। रेस/लेआउट/main.xml (PortraitMode):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="layout-land/PORTRAIT" />
<fragment class="com.example.testapp.fragments.PortraitModeFragment"
android:id="@+id/fragment" android:layout_weight="1"
android:layout_width="0px" android:layout_height="match_parent" android:tag="PortraitModeFragment">
</fragment>
</LinearLayout>
2। रेस/लेआउट भूमि/main.xml (LandscapeMode)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="layout-land/LANDSCAPE" />
<fragment class="com.example.testapp.fragments.LandscapeModeFragment"
android:id="@+id/LandscapeModeFragment" android:layout_weight="1"
android:layout_width="0px" android:layout_height="0dp" android:tag="LandscapeModeFragment">
</fragment>
</LinearLayout>
MainActivity.java:
public class MainActivity extends FragmentActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG, "onCreate()");
setContentView(R.layout.activity_main);
getSupportFragmentManager().dump("", null,
new PrintWriter(System.out, true), null);
}
}
FragmentPortraitMode:
public class PortraitModeFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragment = inflater.inflate(R.layout.portraitmodefragment_layout,
null);
Button findViewById = (Button) fragment.findViewById(R.id.button1);
findViewById.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MyDialogFragment myDialog = new MyDialogFragment();
myDialog.show(getActivity().getSupportFragmentManager(),
MyDialogFragment.TAG);
}
});
TextView textView = (TextView) fragment.findViewById(R.id.textView1);
textView.setText(TAG);
return fragment;
}
टुकड़ा LandscapeMode:
public class LandscapeModeFragment extends Fragment {
public static final String TAG = "LandscapeModeFragment";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View fragment = inflater.inflate(R.layout.landscapemode_fragment_layout,
container, false);
return fragment;
}
}
landscapemode_fragment_layo ut.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LANDSCAPEFRAGMENT" />
</LinearLayout>
portraitmode_fragment_layout_xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
'generic_dialog_layout.xml' क्या है/जहां लैंडस्केपमोडफ्रैगमेंट में फुलाया जा सकता है? – Kerry
@ केरी ने संवाद के लिए लेआउट जोड़ा, नामकरण यहां कमजोर था, मैं स्वीकार करता हूं। यह सिर्फ एक सामान्य लेआउट है ... – AndacAydin
मैंने इस्तेमाल किए गए लेआउट.एक्सएमएल का नाम बदलकर landscape_fragment_layout.xml में बदल दिया है, यह नामकरण के अनुसार स्पष्ट होना चाहिए। – AndacAydin