There is an Activity and Fragment , you need to pass a variable of type int Id from Activity to Fragment for further use, I tried through the bundle and all the time the NullPointerException error crashes and refers to the fact that nothing comes to the variable Id

Attempt to invoke virtual method 'int android.os.Bundle.getInt (java.lang.String)' on a null object reference

Code in Fragment :

 public class MapFragment extends Fragment implements View.OnClickListener private int id; public static MapFragment newInstance(String key,int value) { MapFragment fragment = new MapFragment(); Bundle bundle = new Bundle(); bundle.putInt(key,value); fragment.setArguments(bundle); return fragment; } @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.map_fragment, container, false); id = this.getArguments().getInt("id"); return view; } } 

And the code in the Activity :

 public class ChooseWarActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_choose_war); int id = 15; MapFragment fragmentID = MapFragment.newInstance("id",id); } } 

Tell me how can I do it differently or what is my mistake, otherwise I already broke my head?

UPD

Could this be related to the type of adding a fragment? It seemed to do according to a template and when you press the button, a fragment appears, it suddenly clarifies something

 public class GameActivity extends AppCompatActivity { private MapFragment gameFragment; private FragmentManager manager; private FragmentTransaction transaction; protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.game_layout); manager = getSupportFragmentManager(); gameFragment = new MapFragment(); textView = (TextView) findViewById(R.id.gameInfo); button = (Button) findViewById(R.id.gameStartbtn); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { transaction = manager.beginTransaction(); transaction.add(R.id.container, gameFragment); transaction.commit(); } }); } } 

    4 answers 4

    In your GameActivity you create a snippet through the constructor gameFragment = new MapFragment(); , and not through newInstance() . Accordingly, no arguments fall into it. Hence, when referring to the arguments inside the fragment, you have a NullPointerException . Replace the fragment creation code with the one in your first example:

     ... gameFragment = MapFragment.newInstance("id", id); ... 
    • Indeed, now I understand what's what, thanks - Cyril

    Try so, in the Activity to send the id :

     Bundle bundle = new Bundle(); bundle.putString("id", id); MapFragment mapFragment = new MapFragment(); mapFragment.setArguments(bundle); 

    And getting in the MapFragment:

     Bundle bundle = this.getArguments(); if (bundle != null){ id = getArguments().getInt("id"); } 
    • I tried to do this and he stopped issuing an error, and of course this means that the bundle came empty, and what to do now? - Cyril
    • And how do you understand that it is empty, since I do not see that you (somewhere) used it? - DevOma
    • In the application I tried to use and simply shove id in TextView, as a result he ignored this action and went further along the code, in general the conditions were fulfilled that Bundle == null and the text was not substituted in TextView, and in general if you remove the if (bundle! = null) then again the error - Cyril
    • Tried to announce the Fragment and pass the data in the onClick() method? - DevOma

    In GameActivity you create a snippet directly using the new operator:

     gameFragment = new MapFragment(); 

    instead of using the factory method newInstance(...) . Accordingly, the created fragment has no arguments, as evidenced by the exception:

     Attempt to invoke virtual method 'int android.os.Bundle.getInt(java.lang.String)' on a null object reference 
    • Thank you very much, figured out - Cyril

    Read the error text:

    java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.Bundle.getInt (java.lang.String)' on a null object reference

    Most likely, the error crashes on the line

     id = this.getArguments().getInt("id"); 

    Right?

    The error indicates that the getArguments() method returns null .

    In short, it’s not for nothing that they prohibit you from removing the default constructor from a fragment, and there may not always be arguments, but you don’t have checks.

    Do this:

     Bundle args = this.getArguments(); if (args != null) { /* делайте, что нужно */ } 
    • Yes, you are right about the place of the error, the question of how to eliminate In general, gave the answer to the previous speaker, because. approximately, you asked for the same check "I tried to do this and it stopped producing an error, and of course this means that the bundle came empty, so what to do now?" - Cyril
    • @Kirill not only you call your fragment - I wrote in the answer that it’s not for nothing that you are forced to leave the default constructor. This means that the fragment will not always have arguments. Just do not worry, and just in case, log the case when they are not null . And check already in the application: does everything work as it should? - Peter Samokhin
    • one
      probably because of a lack of knowledge, but I didn’t quite understand your answer, it just sounded like well, maybe there’s no argument and your fragment willn’t, but you will accept it, just anyway there is a problem with the fact that I need to get the value from the ID variable and if it doesn't work like that, what to do? - Cyril