0

New Question abaut this: Android Studio Refresh Error

I want call a Method in a Fragment from another Fragment. I have already tried this:

(Tab3Storage) (getSupportFragmentManager().findFragmentById(R.id.tab3storage)).Storagerefresh();

but I have a NullPointerException error. How I can make this or a calling form a activity to the Fragment, because the calling from the Fragment to the Activity works already fine.

Code:

Main Activity:

public void refreshAll(){

    Tab3Storage tab3Storage = new Tab3Storage();
    tab3Storage.Storagerefresh();

    Tab4Gravel tab4Gravel = new Tab4Gravel();
    tab4Gravel.Gravelrefresh();
}

This is the Fragment Code:

totalMoney = loadData("totalMoney");
    totalMoneyDisplay.setText("$ " + totalMoney);

    totalGravel = loadData("totalGravel");
    totalGravelDisplay.setText(totalGravel + " Gravel");

    Storage_Level = loadData("storageLevel");
    if (Storage_Level == 0){
        Storage_Level = 1;
    }
    if(Storage_Level == 1){
        Storage_Capacity = Storage_Level1;
    }
    if(Storage_Level == 2){
        Storage_Capacity = Storage_Level2;
    }
    if(Storage_Level == 3){
        Storage_Capacity = Storage_Level3;
    }
    if(Storage_Level == 4){
        Storage_Capacity = Storage_Level4;
    }
    saveData("storageLevel", Storage_Level);

    Storage_Filled = totalGravel;
    storageCapacityDisplay.setText(Storage_Filled + "/" + Storage_Capacity);
    SellGravelButton.setText("Sell: $" + totalGravel);

    storageUpgradebtn.setText("Level " + Storage_Level + ":\n$" + Storage_Capacity / 2 );

load Data Code:

private long loadData(String name){

    SharedPreferences shared = this.getActivity().getPreferences(Context.MODE_PRIVATE);
    long value = shared.getLong(name, 0);
    return value;
}
Daniel Maile
  • 13
  • 1
  • 5

3 Answers3

1

What your telling android with this getSupportFragmentManager().findFragmentById(R.id.tab3storage)) is to get the view of the fragment, not actually the code of it

To do that simply create the fragment and call the function:

myFragment fragment = new myFragment();
fragment.Storagerefresh();
Derek
  • 196
  • 1
  • 12
  • This works better, but gives my this error: "java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.support.v4.app.FragmentActivity.getPreferences(int)' on a null object reference" – Daniel Maile Jun 21 '17 at 18:35
  • I think what's happening is `this.getActivity` is returning null, can you check that? – Derek Jun 21 '17 at 18:54
  • @DanielMaile I would pass the activity in the function: `loadData(String name, Activity currentActivity){` – Derek Jun 22 '17 at 14:54
  • How do you mean that, can you please give me more informations? If you want, I can give you the whole Project over Mediafire. – Daniel Maile Jun 22 '17 at 14:58
0
/**Use following Spinet of Code**/

Note: Below "activity_framelayout" is your Activity FrameLayout on which all your related Fragment is Attached. And Here i used "FrameLayout"
you can use any Layout either "RelativeLayout" or "LinearLayout" depends on your choice.

<FrameLayout
        android:id="@+id/activity_framelayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

FrameLayout activity_framelayout = (FrameLayout) findViewById(R.id.activity_framelayout);

Fragment fragment = new Target_Fragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

if(activity_framelayout.getChildCount() > 0 &&   activity_framelayout.getChildAt(0) != null)
{
   activity_framelayout.removeAllViews();
}
fragmentTransaction.add(R.id.activity_framelayout, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
yash786
  • 1,151
  • 8
  • 18
-1

The right solution is described in android documentation. Communicating with Other Fragments.
As per the docs, you can define an interface in the Fragment class and implement it within the Activity, then invoke the interface as per the event you desired, so that receiver side will capture that event.

Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56
frozzyk
  • 475
  • 7
  • 16