AsyncTask 中代码崩溃的旋转屏幕

Posted

技术标签:

【中文标题】AsyncTask 中代码崩溃的旋转屏幕【英文标题】:Rotation screen by code crashes in AsyncTask 【发布时间】:2017-05-16 00:08:36 【问题描述】:

我想通过带有 AsynTask 的代码更改屏幕的方向,但我注意到如果我放置,我的应用程序会崩溃

getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

public String doInBackground(String... params)public void onPostExecute(String s)

相反,此方法适用于例如 TabbedActivity 类或 Fragment 类。为什么这个方法在 AsynTask 中不起作用?我真的需要使用它!

这是日志猫

01-01 18:03:40.953 3646-3783/com.example.utente.progettigscorsipa E/androidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                                                Process: com.example.utente.progettigscorsipa, PID: 3646
                                                                                java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                                    at android.os.AsyncTask$3.done(AsyncTask.java:318)
                                                                                    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                                    at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                                    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                                    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
                                                                                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                                    at java.lang.Thread.run(Thread.java:761)
                                                                                 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.app.FragmentActivity.setRequestedOrientation(int)' on a null object reference
                                                                                    at com.example.utente.progettigscorsipa.FotoCuboActivity$PlaceholderFragment$MultiplyTask.doInBackground(FotoCuboActivity.java:165)
                                                                                    at com.example.utente.progettigscorsipa.FotoCuboActivity$PlaceholderFragment$MultiplyTask.doInBackground(FotoCuboActivity.java:142)
                                                                                    at android.os.AsyncTask$2.call(AsyncTask.java:304)
                                                                                    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243) 
                                                                                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
                                                                                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
                                                                                    at java.lang.Thread.run(Thread.java:761) 

01-01 18:03:41.340 3646-3682/com.example.utente.progettigscorsipa E/EGL_emulation:tid 3682:eglSurfaceAttrib(1146):错误 0x3009 (EGL_BAD_MATCH) 01-01 18:03:41.340 3646-3682/com.example.utente.progettigscorsipa W/OpenGLRenderer:无法在表面 0x9637ab60 上设置 EGL_SWAP_BEHAVIOR,错误=EGL_BAD_MATCH

public static class PlaceholderFragment extends Fragment 
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";
    private boolean ciclo=true;
    private Activity activity;

    public PlaceholderFragment() 
    

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) 
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    
        switch (getArguments().getInt(ARG_SECTION_NUMBER))
        
            case 1:
                SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
                SharedPreferences.Editor editor = prefs.edit();
                editor.putInt("i",0);
                editor.commit();
                new MultiplyTask().execute();
                View rootView = inflater.inflate(R.layout.fragment_foto_cubo, container, false);
                return rootView;

            case 2:
                View rootView2 = inflater.inflate(R.layout.fragment_foto_cubo4, container, false);
                return rootView2;

            case 3:
                View rootView3 = inflater.inflate(R.layout.fragment_foto_cubo5, container, false);
                return rootView3;

            case 4:
                View rootView4 = inflater.inflate(R.layout.fragment_foto_cubo6, container, false);
                return rootView4;

            case 5:
                View rootView5 = inflater.inflate(R.layout.fragment_foto_cubo7, container, false);
                return rootView5;

            case 6:
                View rootView6 = inflater.inflate(R.layout.fragment_foto_cubo8, container, false);
                return rootView6;

            case 7:
                View rootView7 = inflater.inflate(R.layout.fragment_foto_cubo2, container, false);
                return rootView7;

            case 8:
                prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
                editor = prefs.edit();
                editor.putInt("i",7);
                editor.commit();
                new MultiplyTask().execute();
                View rootView8 = inflater.inflate(R.layout.fragment_foto_cubo3, container, false);
                return rootView8;

            default:
                View rootView0 = inflater.inflate(R.layout.fragment_foto_cubo, container, false);
                return rootView0;
        
    

    @Override
    public void onAttach(Activity activity1)
    
        super.onAttach(activity1);
        activity = activity1;

    

    public class MultiplyTask extends AsyncTask<String,String,String>
    
        @Override
        public void onPreExecute()
        
            super.onPreExecute();
            Toast.makeText(getActivity(),"Sto eseguendo",Toast.LENGTH_LONG).show();
        

        @Override
        public void onPostExecute(String s)
        
            super.onPostExecute(s);
        

        @Override
        public String doInBackground(String... params)
        
            SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
            while(ciclo)
            
                if(prefs.getInt("i",0)==7)
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                else
                    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            
            return null;
        


    

【问题讨论】:

为 onPostExecute 放上崩溃的详细信息 @weston 现在可以工作了,但我什么都没做……在我的应用在模拟器和手机上崩溃之前 @weston 但是,我可以将getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 放入doInBackGround 吗? 你说你做到了。并且出现了问题,所以有什么不对吗?,但我不是什么都知道,我依靠抛出的异常的细节来确定问题。更多的代码也会很好。 让我们continue this discussion in chat. 【参考方案1】:

在使用 AsyncTask 时,我将屏幕锁定在当前方向比强制方向(就像你对横向所做的那样)更成功。

//lock screen orientation
private void lockScreenOrientation() 
    int currentOrientation = getResources().getConfiguration().orientation;
    if (currentOrientation == Configuration.ORIENTATION_PORTRAIT) setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    else setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

【讨论】:

我需要强制方向 我遇到了类似的情况,我希望在 AsyncTask 期间屏幕处于纵向。我努力不让它崩溃。我终于用上面的方法将屏幕锁定在现有状态。我建议您在使用 AsyncTask 进行活动之前强制进行定向……也许是有意图的。祝你好运。 我只需要更改两个片段的方向。看看我更新的问题。我解决了崩溃问题

以上是关于AsyncTask 中代码崩溃的旋转屏幕的主要内容,如果未能解决你的问题,请参考以下文章

安卓片段。在屏幕旋转或配置更改期间保留 AsyncTask

Android 屏幕旋转 处理 AsyncTask 和 ProgressDialog 的最佳方案

Android 屏幕旋转 处理 AsyncTask 和 ProgressDialog 的最佳方案

Android 屏幕旋转 处理 AsyncTask 和 ProgressDialog 的最佳方案

获得致命异常:使用 Async 类的 AsyncTask #1

async Task.Run lambda 表达式在 Android 上使 Unity AR 应用程序崩溃,但在 iOS 上运行良好