David
David

Reputation: 3105

android fragment interface nullpointerexception

I have two fragments and I want to update second fragment TextView from first fragment. I desided to use interface

public interface LogTextUpdater {
    void updateText(Information info);
}

Here's my first fragment

public class UiFragment extends Fragment {
    public LogTextUpdater log;
    ...
    ...
    private void setInformationObject(String message){
        Information info = null;
        try {
            info = new XmlParser(message).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        if(info != null){
            log.updateText(info); // here I update second fragment TextView
        }
    }
}

and here's my second fragment

public class LogFragment extends Fragment implements LogTextUpdater{

    private TextView logView;
    private UiFragment fragment;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fragment = new UiFragment();
        fragment.log = this; // register 
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_log, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        logView = (TextView) view.findViewById(R.id.textLog);
    }

    @Override
    public void updateText(Information info) {
        logView.setText(info.getName() + " " + info.getCity());
    }
}

Here's my MainActivity

public class MainActivity extends FragmentActivity {
 private PagerAdapter mPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initialisePaging();
    }

    private void initialisePaging() {
        List<Fragment> fragments = new Vector<>();
        fragments.add(Fragment.instantiate(this, UiFragment.class.getName()));
        fragments.add(Fragment.instantiate(this, LogFragment.class.getName()));
        mPagerAdapter = new PagerAdapter(getSupportFragmentManager(), fragments);
        ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
        pager.setAdapter(mPagerAdapter);
    }
}

and here's my error

java.lang.NullPointerException
            at carproject.hfad.com.carproject.UiFragment.setInformationObject(UiFragment.java:289)
            at carproject.hfad.com.carproject.UiFragment.sendCommand(UiFragment.java:313)
            at carproject.hfad.com.carproject.UiFragment.access$300(UiFragment.java:39)
            at carproject.hfad.com.carproject.UiFragment$1.onClick(UiFragment.java:125)
            at android.view.View.performClick(View.java:4654)
            at android.widget.CompoundButton.performClick(CompoundButton.java:104)
            at android.view.View$PerformClick.run(View.java:19438)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5602)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)

After debug I found that log is null! but why? I register interface in LogFragment fragment.log = this; // register

Upvotes: 1

Views: 627

Answers (1)

Zsolt Mester
Zsolt Mester

Reputation: 1053

Fragment to fragment communication is not allowed. Please check the official documentation. The key sentence:

Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly.

What you should do

Your MainActivity should implement the LogTextUpdater. Then in the updateText method you should find the LogFragment and pass the Information to it.

Upvotes: 1

Related Questions