I keep getting a NullPointer
from my bundle but it only started when I changed how I was manipulating a fragment to I think it has to do with the transaction in the menu but I can't find anything on what the problem could be.
This is the error (It's long but this has the two spots it shows to have crashed at).
W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.get(java.lang.String)' on a null object reference 01-22 13:58:05.652 28461-28461/com.nyc.javadontlie
W/System.err: at com.nyc.javadontlie.LoggingFragment.onViewCreated(LoggingFragment.java:59) 01-22 13:58:05.652 28461-28461/com.nyc.javadontlie
W/System.err: at android.support.v4.app.FragmentManagerImpl.ensureInflatedFragmentView(FragmentManager.java:1651) 01-22 13:58:05.652 28461-28461/com.nyc.javadontlie
W/System.err: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1390) 01-22 13:58:05.652 28461-28461/com.nyc.javadontlie
W/System.err: at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1640) 01-22 13:58:05.653 28461-28461/com.nyc.javadontlie
W/System.err: at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1896) 01-22 13:58:05.653 28461-28461/com.nyc.javadontlie
W/System.err: at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3673) 01-22 13:58:05.653 28461-28461/com.nyc.javadontlie
W/System.err: at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111) 01-22 13:58:05.653 28461-28461/com.nyc.javadontlie
W/System.err: at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:338) 01-22 13:58:05.653 28461-28461/com.nyc.javadontlie
W/System
My Logging/fragment
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log; import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.nyc.javadontlie.controller.LoggingAdapter;
import java.util.ArrayList; import java.util.Collections;
import static android.content.Context.MODE_PRIVATE;
/** * A simple {@link Fragment} subclass. */
public class LoggingFragment extends Fragment {
private RecyclerView recyclerView;
private ArrayList<String> logArrayList;
private Bundle bundle;
public LoggingFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_logging, container, false);
bundle = getArguments();
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
SharedPreferences gameInfo = getActivity().getApplicationContext().getSharedPreferences("GameModel",MODE_PRIVATE);
logArrayList = new ArrayList<>();
try {
String gameName;
if (!bundle.get(Constants.LOGGING_FRAG_KEY).equals(null)) {
gameName = bundle.get(Constants.LOGGING_FRAG_KEY).toString();
} else {
gameName = "";
}
if (gameInfo.getString("StringLog" + gameName, null) != null) {
String[] stringSet = gameInfo.getString("StringLog" + gameName, null).trim().split(",");
Collections.addAll(logArrayList, stringSet);
}
} catch (NullPointerException e){
e.printStackTrace();
}
recyclerView = view.findViewById(R.id.recyclerView_logging);
LoggingAdapter adapter = new LoggingAdapter(logArrayList);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext(),LinearLayout.VERTICAL,false);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(layoutManager);
} }
This is where the transaction is made.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.log_text_menu:
LoggingFragment fragment = (LoggingFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_container);
if (fragment == null) {
Log.d(TAG, "Log was Clicked");
addLogToShared();
fragment = new LoggingFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
fragment.setArguments(bundle);
transaction.replace(fragment_container,fragment);
transaction.addToBackStack("LogFrag");
transaction.show(fragment);
transaction.commit();
} else if (fragment.isVisible()){
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.hide(fragment);
transaction.commit();
} else if (!fragment.isVisible()) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
fragment.setArguments(bundle);
transaction.show(fragment);
transaction.commit();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void addLogToShared() {
SharedPreferences.Editor editor = gameInfo.edit();
StringBuilder s = new StringBuilder();
for (String s1: logArrayList) {
s.append(s1).append(",");
}
editor.putString("StringLog" + gameName, s.toString());
editor.apply();
}