This question has already been answered:

There is a layout file that acts as a container:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.test.test.MainActivity"> </FrameLayout> 

There is also a java-class in which the method is called from another class:

 import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { List fragList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Иницилизация второго java-класса fragList = (List) getSupportFragmentManager().findFragmentById(R.id.frameLayout); } @Override public void onBackPressed() { //Вызов метода из другого класса fragList.enterBackButton(); } } 

Finally, there is a class that must handle the method from the first class (MainActivity):

 import android.content.Context; import android.os.Bundle; import android.view.View; import android.widget.ArrayAdapter; import android.widget.ListView; public class List extends android.support.v4.app.ListFragment { ArrayAdapter<CharSequence> adapter1, adapter2, adapter3; @Override public void onAttach(Context context) { super.onAttach(context); //Инициализация адаптеров adapter1 = new ArrayAdapter<CharSequence>(getActivity(), android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.test1)); adapter2 = new ArrayAdapter<CharSequence>(getActivity(), android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.test2)); adapter3 = new ArrayAdapter<CharSequence>(getActivity(), android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.test3)); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //Установка начального адаптера setListAdapter(adapter1); } @Override public void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); //При выборе пункта списка адаптер меняется на другой if (getListAdapter() == adapter1) { switch (position) { case 0: setListAdapter(adapter2); break; case 1: break; } } else if (getListAdapter() == adapter2) { switch (position) { case 0: setListAdapter(adapter3); break; case 1: break; } } } //Этот метод должен заменить onBackPressed(), объявленный в другом классе public void enterBackButton () { if (getListAdapter() == adapter2) { setListAdapter(adapter1); } else if (getListAdapter() == adapter3) { setListAdapter(adapter2); } } } 

When you start the application, a list should appear, but this does not happen. In addition, when you press the "back" button, the program ends with an NPE error.

Reported as a duplicate by the participants YuriyiPb , pavlofff , user194374, Saidolim , Abyx 22 Jan '16 at 17:01 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • one
    I don’t understand how and why this should work .. - pavlofff
  • one
    Burn this code and do not show it to anyone))) - MrFirst

2 answers 2

This is the third, in my memory, your question on the same topic with the same initial error. For the third time I repeat:

A fragment declared but not added to the activation cannot work with its internal dimension, since I did not upload it, because was not added to the activation.

You need:

  1. Do not add fragment to markup in markup. Do this in code through FragmentManager
  2. Through the FragmentManager, you can find it in the markup and call the necessary methods on the fragment obtained in this way.

UPD_0:

Because you did not add the fragment before searching for it, and it turns out to be null

So check for a null found fragment and add it if it is not found.

 fragList = (List) getSupportFragmentManager().findFragmentById(R.id.frameLayout); if(fragList == null) { fragList = new List(); getSupportFragmentManager().beginTransaction().add(R.id.frameLayout, fragList).commit(); } 
  • one
    @Win_D yes, this is exactly what I mean. And your main mistake is that you need to receive the fragment then through fragmentManager.findFragmentById and not through the new List - YuriySPb
  • one
    @Win_D, you completely misunderstood. You have once added a fragment to the activit. then you get this fragment added via fragmentManager.findFragmentById . Further you call to -l his method and change the list adapters. No assemblies of classes and markup are needed) - YuriySPb
  • one
    @Win_D, if you look in the documentation, you will see that the argument-ID is the container ID in which the fragment is placed. Ie, in your case, the fragment will be as follows: List fragList = getSupportFragmentManager().findFragmentById(R.id.АЙДИШНИК_ЛИНЕАР_ЛЭЙАУТА_СОДЕРЖАЩЕГО_ФРАГМЕНТ); - Yuriy SPb
  • one
    @Win_D, you can not access the activation fragment before it, activate, create. During the initialization of the class variable, the activation class is not yet displayed and there are few fragments in it. You need to transfer this code to where you need this fragment and when it has already been added. In your case, onBackPressed . This is where you need to assign a variable to a fragment found in the FragmentManager . And yes, you need to cast it to your type. Those. put (List) before getSupport........ - YuriySPb
  • one
    @Win_D, there is not enough context for your error. You may not have the right Aidi indicated (you had Linear, like). Maybe you did not add the fragment to the activation before its search. Maybe your method in the fragment is incorrect, but it is still called. Update the question. Provide complete and up-to - date information - YuriySPb
  1. You incorrectly compare objects.
    getListAdapter() == adapter1 - this cannot be done, as a result, conditions are not met
    if -> else if
    learn mat part
  2. You call a method on an instance that has nothing to do with your activation, get a snippet like this:
    List listFragment = getFragmentManager (). FindFragmentById (id);