When I go to click an item in the SettingsFragment, my app crashes. Everything is displayed properly in this fragment class but only my parseUrl items work. i click on any other items and my app just closes out. Why does that happen? Below is my code.
public class SettingsFragment extends Fragment {
MainActivity context;
public SettingsFragment() {
//public constructor
}
@NonNull
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.navigation_drawer, container, false);
FrameLayout instructions = view.findViewById(R.id.instructions);
if (!getInstance().get("INSTRUCTIONS_ACTIVE", true)) {
instructions.setVisibility(View.GONE);
}
instructions.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openInstructions();
}
});
return view;
}
// Main functions
void openInstructions(){
Intent transactions = new Intent(context, FragmentsActivity.class);
transactions.putExtra("show","instructions");
startActivity(transactions);
}
public void settingsMenu(String Type){
switch (Type) {
case "instructions":
openInstructions();
break;
}
It seems that the context is null
Use getContext()
or getActivity()
inside onCreateView
to initialize the context
context is not initialized, one way to do that is to add this method:
@Override
public void onAttach(Context ctx) {
super.onAttach(ctx);
context = ctx;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.