Good day. I'm trying to use fragments in the application. The first fragment displays the image, the second text. At startup, a fragment with a picture is set, when you select Drawer in the side menu, the picture in the fragment is set, everything is fine, then I click on the button in the activation ( onClick method), the fragment with the picture is replaced with the second fragment with text and the work is done there, everything is fine too. But then when I click the item in the side menu again, I get an NPE error in the line ((ImageView) frag.getView().findViewById(R.id.fragment_horoicon)).setImageResource(R.drawable.oven); (also indicated with a comment in the code), although it seems that before switching to a switch he made a change of a fragment with text to a fragment with a picture. Please tell me where the error lies? Thank.
PS MaterialDrawer is used for side menu
MainActivity.java
import android.app.Fragment; import android.app.FragmentTransaction; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import com.mikepenz.materialdrawer.Drawer; import com.mikepenz.materialdrawer.DrawerBuilder; import com.mikepenz.materialdrawer.model.DividerDrawerItem; import com.mikepenz.materialdrawer.model.PrimaryDrawerItem; import com.mikepenz.materialdrawer.model.SecondaryDrawerItem; import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import java.io.IOException; public class MainActivity extends AppCompatActivity { Drawer drawer; Fragment_image frag_image; Fragment_text frag_text; FragmentTransaction frag_trans; Button button_yesterday; String link; String prelink; Parser par; String text; String date; Fragment frag; public void onClick(View view) { frag_trans = getFragmentManager().beginTransaction(); frag_trans.replace(R.id.frag_container, frag_text); // Π·Π°ΠΌΠ΅Π½ΡΠ΅ΠΌ ΡΡΠ°Π³ΠΌΠ΅Π½Ρ Ρ ΠΊΠ°ΡΡΠΈΠ½ΠΊΠΎΠΉ Π½Π° ΡΡΠ°Π³ΠΌΠ΅Π½Ρ Ρ ΡΠ΅ΠΊΡΡΠΎΠΌ frag_trans.commit(); switch (view.getId()) { case R.id.button_yesterday: link = prelink + "yesterday.html"; break; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); frag_image = new Fragment_image(); frag_text = new Fragment_text(); button_yesterday = (Button) findViewById(R.id.button_yesterday); frag_trans = getFragmentManager().beginTransaction(); frag_trans.add(R.id.frag_container, frag_image); // ΡΡΡΠ°Π½Π°Π²Π»ΠΈΠ²Π°Π΅ΠΌ ΠΏΠ΅ΡΠ²ΡΠΉ ΡΡΠ°Π³ΠΌΠ΅Π½Ρ Ρ ΠΊΠ°ΡΡΠΈΠ½ΠΊΠΎΠΉ frag_trans.commit(); frag = getFragmentManager().findFragmentById(R.id.frag_container); PrimaryDrawerItem item1 = new PrimaryDrawerItem().withIdentifier(1).withName(R.string.select_sign).withSelectable(false); SecondaryDrawerItem item2 = (SecondaryDrawerItem) new SecondaryDrawerItem().withIdentifier(2).withName(R.string.oven); drawer = new DrawerBuilder() .withActivity(this) .withHeader(R.layout.drawer_header) .withTranslucentStatusBar(true) .withActionBarDrawerToggleAnimated(true) .withSelectedItem(-1) .addDrawerItems( item1, new DividerDrawerItem(), item2 ) .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() { @Override public boolean onItemClick(View view, int position, IDrawerItem drawerItem) { frag_trans = getFragmentManager().beginTransaction(); frag_trans.replace(R.id.frag_container, frag_image); // ΠΏΡΠΈ ΠΊΠ»ΠΈΠΊΠ΅ Π² Π±ΠΎΠΊΠΎΠ²ΠΎΠΌ ΠΌΠ΅Π½Ρ ΠΏΠΎ ΠΏΡΠ½ΠΊΡΡ ΡΠΏΠΈΡΠΊΠ° ΡΡΡΠ°Π½Π°Π²Π»ΠΈΠ²Π°Π΅ΠΌ ΡΡΠ°Π³ΠΌΠ΅Π½Ρ Ρ ΠΊΠ°ΡΡΠΈΠ½ΠΊΠΎΠΉ ΠΈ ΠΏΠΎΠ΄Π³ΡΡΠΆΠ°Ρ ΠΊΠ°ΡΡΠΈΠ½ΠΊΡ Π² ImageView frag_trans.commit(); frag = getFragmentManager().findFragmentById(R.id.frag_container); switch ((int) drawerItem.getIdentifier()) { case 2: ((ImageView) frag.getView().findViewById(R.id.fragment_horoicon)).setImageResource(R.drawable.oven); // ΡΡΡ ΠΏΠΎΠ»ΡΡΠ°Ρ NPE break; default: break; } return true; } }) .build(); } } UPD
public class MainActivity extends AppCompatActivity { Fragment_image frag_image; Fragment_text frag_text; FragmentTransaction frag_trans; Fragment frag; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); frag_image = new Fragment_image(); frag_text = new Fragment_text(); frag_trans = getFragmentManager().beginTransaction(); frag_trans.add(R.id.frag_container, frag_image); frag_trans.commit(); getFragmentManager().executePendingTransactions(); frag = getFragmentManager().findFragmentById(R.id.frag_container); Handler handler = new Handler(); handler.postDelayed(new Runnable() { public void run() { ((ImageView)frag.getView().findViewById(R.id.fragment_horoicon)).setImageResource(R.drawable.choose_sign); } }, 100); } }
frag_trans.replace(R.id.frag_container, frag_image);If the first time the picture works, then everything is fine with the fragment class itself. Previously, the fragments did not work, so the suspicion that somewhere I make a mistake in the principles of changing fragments. - PolluxcommitNowthere is no such method ... there is only.commitAllowingStateLoss(). I useandroid.app.Fragment. Regarding the appeal to the markup, I did this lesson - PolluxgetFragmentManager().executePendingTransactions();and it all worked. Please write as an answer, I will note. - Pollux