Android应用程序因方向更改而崩溃

时间:2016-09-25 22:14:20

标签: java android android-fragments

该应用程序有一个主要活动和两个片段。主要活动主机MainFragmentMainFragment包含提交BreedingFragment(运行)的按钮。只有在方向未更改时应用程序才能正常工作,但在BreedingFragment打开且方向发生变化时崩溃。

在对 stackoverflow 进行了几个小时的调查后,我找到了以下文章并按照答案进行了调查,但没有调试问题。

  1. Android Application Crashes With Orientation Changes
  2. Orientation change Crash Application
  3. Rotation of screen crashes android app
  4. My Android application crashes when I change screen orientation
  5. 答案主要建议在AndroidManifest.xml中使用以下代码:

    android:configChanges="orientation|keyboardHidden|screenSize"
    

    最后一个帖子建议考虑以下因素:

      

    每次设备活动都会停止并重新启动   方向变化。你需要写下你的线程,   即停止线程并在设备更改时重新启动它   取向,也许是在两者之间挽救国家。

    MainActivity:

    public class MainActivity extends FragmentActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    }
    

    MainFragment:

    public class MainFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
            View v = inflater.inflate(R.layout.fragment_main, container, false);
    
            Button breedButton = (Button) v.findViewById(R.id.pokemon_breeding_button);
    
            breedButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                    final FragmentTransaction transaction = fragmentManager.beginTransaction();
                    transaction.replace(R.id.fragment, new BreedingFragment());
                    transaction.addToBackStack(null);
                    transaction.commit();
                }
            });
            return v;
        }
    }
    

    BreedingFragment:

    public class BreedingFragment extends Fragment {
    
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
            return inflater.inflate(R.layout.fragment_breeding, container, false);
        }
    }
    

    Log Cat:

    E/AndroidRuntime: FATAL EXCEPTION: main
                      Process: com.revittechnology.zapdos, PID: 12795
                      java.lang.RuntimeException: Unable to start activity ComponentInfo{com.revittechnology.zapdos/com.revittechnology.zapdos.MainActivity}: android.view.InflateException: Binary XML file line #24: Error inflating class fragment
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
                          at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4014)
                          at android.app.ActivityThread.access$900(ActivityThread.java:166)
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
                          at android.os.Handler.dispatchMessage(Handler.java:102)
                          at android.os.Looper.loop(Looper.java:136)
                          at android.app.ActivityThread.main(ActivityThread.java:5590)
                          at java.lang.reflect.Method.invokeNative(Native Method)
                          at java.lang.reflect.Method.invoke(Method.java:515)
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1280)
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1096)
                          at dalvik.system.NativeStart.main(Native Method)
                       Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class fragment
                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:720)
                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:762)
                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:771)
                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:771)
                          at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
                          at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
                          at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
                          at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:331)
                          at android.app.Activity.setContentView(Activity.java:2018)
                          at com.revittechnology.zapdos.MainActivity.onCreate(MainActivity.java:13)
                          at android.app.Activity.performCreate(Activity.java:5447)
                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
                          at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4014) 
                          at android.app.ActivityThread.access$900(ActivityThread.java:166) 
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289) 
                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                          at android.os.Looper.loop(Looper.java:136) 
                          at android.app.ActivityThread.main(ActivityThread.java:5590) 
                          at java.lang.reflect.Method.invokeNative(Native Method) 
                          at java.lang.reflect.Method.invoke(Method.java:515) 
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1280) 
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1096) 
                          at dalvik.system.NativeStart.main(Native Method) 
                       Caused by: java.lang.IllegalStateException: Fragment com.revittechnology.zapdos.MainFragment did not create a view.
                          at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2319)
                          at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
                          at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:356)
                          at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
                          at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:762) 
                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:771) 
                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:771) 
                          at android.view.LayoutInflater.inflate(LayoutInflater.java:499) 
                          at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
                          at android.view.LayoutInflater.inflate(LayoutInflater.java:354) 
                          at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:331) 
                          at android.app.Activity.setContentView(Activity.java:2018) 
                          at com.revittechnology.zapdos.MainActivity.onCreate(MainActivity.java:13) 
                          at android.app.Activity.performCreate(Activity.java:5447) 
                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 
                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393) 
                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
                          at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4014) 
                          at android.app.ActivityThread.access$900(ActivityThread.java:166) 
                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289) 
                          at android.os.Handler.dispatchMessage(Handler.java:102) 
                          at android.os.Looper.loop(Looper.java:136) 
                          at android.app.ActivityThread.main(ActivityThread.java:5590) 
                          at java.lang.reflect.Method.invokeNative(Native Method) 
                          at java.lang.reflect.Method.invoke(Method.java:515) 
                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1280) 
                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1096) 
                          at dalvik.system.NativeStart.main(Native Method) 
    I/Process: Sending signal. PID: 12795 SIG: 9
    Application terminated.
    

1 个答案:

答案 0 :(得分:0)

Main Activity and Main Fragment. 上面的照片详细说明了这个案例。托管MainActivity.java的{​​{1}}和主片段有一个按钮,用于启动另一个片段。

我的要求是什么?

  1. 主要活动只承载主片段,但没有 别的什么。
  2. Main Fragment具有启动第二个片段的按钮。
  3. 按下后退按钮时关闭(结束/完成)第二个片段 (返回主要活动)。
  4. 使用设备(屏幕)旋转。
  5. 第一步是创建主要活动。主要活动中考虑的内容很少:

    1. MainFragment.java扩展为MainActivity
    2. FragmentActivity如下:

      1. 检查public class MainActivity extends FragmentActivity,如果是savedInstanceState,请运行。{ MainFragment。我们这样做的原因是为了避免再次运行 以及设备轮换的MainFragment。
      2. 以下是 null 的代码:

        MainActivity.java

        确保在 public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { FragmentManager fManager = getSupportFragmentManager(); FragmentTransaction fTransaction = fManager.beginTransaction(); fTransaction.add(R.id.fragment, new MainFragment()); fTransaction.commit(); } } }

        中包含以下代码行
        activity_main.xml

        现在创建 <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:name="com.revittechnology.zapdos.MainFragment" android:id="@+id/fragment" />

        MainFragment.java

        在返回视图(public class MainFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_main, container, false); // Inflate the layout for this fragment Button breedButton = (Button) v.findViewById(R.id.pokemon_breeding_button); breedButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final FragmentManager fragmentManager = getActivity().getSupportFragmentManager(); final FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.fragment, new BreedingFragment()); transaction.addToBackStack("tag"); transaction.commit(); } }); return v; } } )之前包含所有代码,因为如果视图不存在,则无法找到按钮和布局的其他元素。 return v有助于将片段添加到后台堆栈中,并允许返回以通过反压来关闭片段。

        现在创建第二个片段 BreedingFragment.java`

        transaction.addToBackStack("tag");

        注意:在声明课程之前,请导入上述课程所需的库。

        结论:应用程序因设备方向而崩溃,因为第二个片段是通过按钮点击启动的,MainFragment是MainActivity的主机。使用设备方向时,片段已关闭,public class BreedingFragment extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_breeding, container, false); } } 中没有代码可再次启动片段,因此,它提供了空的异常错误。将片段相关代码添加到MainActivity.java中以启动片段以编程方式解决问题。