简体   繁体   中英

How to use the interface for the fragment?

I used the broadcast that checks items from the server.And the broadcast is depending AlarmManager that every 2 seconds updated.

this my class for the interface:

public class PollServiceUtils {

private static RefreshLayout mRefreshLayout;


 public interface RefreshLayout{
        void updateUI(boolean isCatch);
    }

public static void getRefreshAlarm(Context context) {



        if (Connectivity.getNetworkInfo(context) != null) {

            String changeRate = SharedPreferenceValues.getChangeRate(context);
            List<Gold> listGold;
            if (changeRate != null && !changeRate.isEmpty()) {
                listGold = new ConnectionDovizCom().fetchGoldValues(context);

                if (listGold.size() == 0) {
                    return;
                }
                SharedPreferenceValues.setValuesGold(context,listGold);
                Intent i = MainPageActivity.newIntent(context, true);
                PendingIntent pi = PendingIntent.getService(context, 1, i, PendingIntent.FLAG_UPDATE_CURRENT);
                //get son change rate
                String resultChangeRate = listGold.get(0).getmChangeRate();
                Log.d(TAG, "RESULT CHANGE  RESULT: " + resultChangeRate);

                if (changeRate.equals(resultChangeRate)) {
                    Log.d(TAG, "Old Change Rate ");
                } else {
                    Log.d(TAG, "New Change Rate ");
                    SharedPreferenceValues.setChangeRate(context, resultChangeRate);
                    mRefreshLayout.updateUI(true);


                }


            }


        }


    }

Then, I called the interface for activity (or fragment) for updating:

public class MainPageActivity extends AppCompatActivity implements View.OnClickListener, AllAsycnTask.AltınAsyncRefreshResponse,PollServiceUtils.RefreshLayout {


/*... a lot of code*/

  @Override
    public void updateUI(boolean isCatch) {
        fm = getSupportFragmentManager();
        fragmentTransaction = fm.beginTransaction();
        fragmentTransaction.replace(R.id.gold_layout, new GoldFragment());
        fragmentTransaction.commit();
    }
}

OR

for the fragment :

public class GoldFragment extends Fragment implements View.OnClickListener,PollServiceUtils.RefreshLayout {

 /*... a lot of code*/ 

  public void updateUIforRefresh(Context c) {

        mGoldsList.clear();
        mGoldsList = SharedPreferenceValues.getValuesGold(c);

        getAltınListForAdapter(c,mGoldsList); // set all view again

    }


 @Override
    public void updateUI(boolean isCatch) {
      updateUIforRefresh(getContext());
    }
}

My problem is that when this connection is done, the interface returns true for updating the fragment. But the logcat shows " Attempt to invoke the interface method on a null object reference for PollServiceUtils"

Do you know which details I snatch?

You must initialize RefreshLayout.mRefreshLayout. You're trying to call your method in interface but interface variable isn't initialized(so default null - lazy initialization).

If you made private interface variable, you must write setter method to set this variable and then call updateUI() method.

Setter:

`public void setRefreshLayoutListener(RefreshLayout rl) {
    mRefreshLayout = rl;
}`

Remember to set this event listener before you will broadcast events.

Cheers, gjm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM