java.lang.NoSuchMethodException for onCreate

后端 未结 4 995
自闭症患者
自闭症患者 2020-12-31 09:38

I see crashes in the Google Play crash log that is really stumping me.

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity (Ac         


        
4条回答
  •  失恋的感觉
    2020-12-31 10:19

    After some search i finally fix the issue. You have to check 3 things.

    1. You should have a 0-arg constructor in the fragment, best practice is to do something like bellow
    2. If you are using callback in the caller, you have to check if getContext is null or not (otherwise you will get a NullPointerException)
    3. Don't forget to test the case when the screen orientation change, this will allow you to reproduce some potential issue due to restoring fragment state

    Sample code example :

        public class MyDialogFragment extends DialogFragment{
         private String id;
    
         public static MyDialogFragment newInstance(String id) {
            MyDialogFragment f = new MyDialogFragment ();
    
            Bundle args = new Bundle();
            if(id!= null){
                args.putString("id", id);
            }
            f.setArguments(args);
    
            return f;
        }
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if(savedInstanceState != null){
                id= savedInstanceState.getString("id");
            }
        }
        }
    

提交回复
热议问题