0

I get the following error which I think means my activity is null

fragment not attached to activity on volley response

How can I fix this issue? These are my codes

  MainActivity._MainActivity_.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(MainActivity._MainActivity_, "inserted", Toast.LENGTH_SHORT).show();
                        }
                    });
                   Activity activity = getActivity();
                   if(activity != null){
                        Intent mainIntent = new Intent(define, SaveNewFactor.class);
                        startActivity(mainIntent);
                    }else {
                        Log.i("myLog","activity is null");
                    }

my logcat image

2 Answers2

0

i haven`t see you fragment in your code..
but i think.. maybe your network response comes too late, and after your fragment detach

0

Change the Intent to start the activity SaveNewFector.class

Intent intent=new Intent(SheetSaveNewFector.this,SaveNewFector.class);  
startActivity(intent);  
Hardik Vegad
  • 111
  • 1
  • 7
  • Thank you. But SheetSaveNewFactor is not a Activity . SheetSaveNewFactor extends BottomSheetDialogFragment – Mohammad Kazemian Nov 05 '19 at 09:25
  • BottomSheetDialogFragment is itself had a context like dialog for that you can use only "this" to get the context, so in that case you might use getContent() for starting new activity, Like Intent intent = new Intent(getContext(),SaveNewFector.class); I suggest put some debug points and check on which exect line you are getting this crash. – Hardik Vegad Nov 05 '19 at 09:33