1

i'm trying to find a fragment that i created by tag

tachoFrag = (menu1_Fragment) getFragmentManager().findFragmentByTag("Tacho");

overviewFrag= (menu2_Fragment)getFragmentManager().findFragmentByTag("Overview");

dataLogFrag = (menu3_Fragment) getFragmentManager().findFragmentByTag("Datalogger");

transaction = getFragmentManager().beginTransaction();

but when i try to replace the content of the frame i get a null problem because the fragments are not found .... Help?

Ferrmolina
  • 2,737
  • 2
  • 30
  • 46
Anoir Nechi
  • 81
  • 1
  • 10
  • 1
    You should post more code and your logcat too. – issathink Apr 20 '15 at 23:16
  • log cat : 'java.lang.NullPointerException: Attempt to invoke virtual method 'void app.z0nen.slidemenu.menu3_Fragment.setData(double, int, int, double)' on a null object reference at app.z0nen.slidemenu. MyActivity$2.handleMessage (MyActivity.java:270)' – Anoir Nechi Apr 20 '15 at 23:19
  • ' if (tachoFrag != null && tachoFrag.isVisible()) { tachoFrag.setRevs(revs); tachoFrag.setSpeed(speed); tachoFrag.setFuelGauge(fuel); } transaction.replace(R.id.container, tachoFrag); transaction.commit(); } ' – Anoir Nechi Apr 20 '15 at 23:20
  • 1
    You should look here - http://stackoverflow.com/questions/20825600/findfragmentbytag-returns-null-after-perform-a-fragmenttransaction-using-repla and also http://stackoverflow.com/questions/18229877/why-is-getfragmentmanager-findfragmentbytagstring-valueofsome-integer-not – TechnoBlahble Apr 20 '15 at 23:21

2 Answers2

1

try below codes

MyFragment fragment= (MyFragment)getFragmentManager().findFragmentByTag("FRAGMENT_TAG");
if(null != fragment  && fragment.isVisible()){
    //execute your codes here

}
Genevieve
  • 450
  • 3
  • 9
  • Welcome to Stack Overflow! While this answer is probably correct and useful, it is preferred if you include some explanation along with it to explain how it helps to solve the problem. This becomes especially useful in the future, if there is a change (possibly unrelated) that causes it to stop working and users need to understand how it once worked. – Kevin Brown-Silva Apr 21 '15 at 00:03
  • ' transaction = getFragmentManager().beginTransaction(); transaction.replace(R.id.container, new menu1_Fragment(),"Tacho"); transaction.commit(); tachoFrag = (menu1_Fragment) getFragmentManager().findFragmentByTag("Tacho");' but the logcat keep showing me this error Attempt to invoke virtual method 'void app.z0nen.slidemenu.menu1_Fragment.setRevs(int)' on a null object reference – Anoir Nechi Apr 21 '15 at 15:13
0

In your code transaction.replace(R.id.container, tachoFrag); you have not provided the TAG.

it should be transaction.replace(R.id.container, tachoFrag, "TAG");

so that you can use getFragmentManager().findFragmentByTag("TAG")

luckylukein
  • 819
  • 1
  • 7
  • 17
  • There also seems to be something wrong. You are checking if `tachoFrag.isVisible()` and calling a few methods and then calling replace on the same fragment. Why? – luckylukein Apr 21 '15 at 00:03