0

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?

lscodex
  • 180
  • 1
  • 6
  • 17
  • 1
    where do you initialize the `PollServiceUtils.mRefreshLayout` ? , are your `updateUI()` method run .. you can check that with `Log.d()` ? – Ali Asadi Feb 02 '18 at 09:32
  • I think I initialize mRefreshLayout from the PollServiceUtils class. and I added value that boolean true.Then, I try to call it from activity or fragment. and I checked log.d for updateUI. But app crash before. So I think, it didn't till come up there. – lscodex Feb 02 '18 at 09:41
  • @AliEsaAssadi you were right. thanks. – lscodex Feb 02 '18 at 11:12
  • @iscodex great , you can vote-up the comment .. have a good day – Ali Asadi Feb 02 '18 at 13:44

1 Answers1

0

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

gjm
  • 49
  • 2
  • Thanks, dude. I checked it on the internet after you say it. I found this [link](https://stackoverflow.com/a/19027202/6235701). – lscodex Feb 02 '18 at 11:11