1

I am demo about communicate in two fragments in the parent activity. This is my code

WebListFragment

public class WebListFragment extends Fragment implements AdapterView.OnItemClickListener {

    private List<String> list = new ArrayList<String>();
    private ListView listView;
    private ArrayAdapter<String> adapter;
    ListItemListener itemListener;

    public interface ListItemListener{
        void itemClick(String url);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        itemListener = (ListItemListener) activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.web_list_fragment, container, false);

        listView = (ListView) view.findViewById(R.id.listWeb);
        list.add("https://www.youtube.com/");
        list.add("http://news.zing.vn/");
        list.add("http://hdonline.vn/");

        adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(this);

        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        itemListener.itemClick(list.get(position));
    }

} 

WebContentFragment

public class WebContentFragment extends Fragment{
    WebView webView;
    View view ;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = (LinearLayout)inflater.inflate(R.layout.web_content_fragment, container, false);
         webView = (WebView) this.getView().findViewById(R.id.webView);
        return view;
    }

    public void setUrl(String url, Context context){

        webView.loadUrl(url);
        Log.d("FF", "ABC");
    }

}

WebFragmentActivity

public class WebFragmentActivity extends AppCompatActivity implements WebListFragment.ListItemListener{

    private Fragment fragmentWebList;
    private Fragment fragmentWebContent;

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

    }

    @Override
    public void itemClick(String url) {
        WebContentFragment fragment = new WebContentFragment();
        fragment.setUrl(url,this);
    }
}

The problem which i have that when call method setUrl in WebContentFragment causes null pointer exception in LogCat. when i debug in this method i saw view and webView equal null although i assigned them onCreateView. I don't know why happened that. Can you show me the reason causes and the solution for this problem. Thanks

My logcat:

java.lang.NullPointerException
            at com.nguyen.fragment.WebFragment.WebContentFragment.setUrl(WebContentFragment.java:61)
            at com.nguyen.fragment.WebFragment.WebFragmentActivity.itemClick(WebFragmentActivity.java:24)
            at com.nguyen.fragment.WebFragment.WebListFragment.onItemClick(WebListFragment.java:55)
            at android.widget.AdapterView.performItemClick(AdapterView.java:298)
            at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
            at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855)
            at android.widget.AbsListView$1.run(AbsListView.java:3529)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

3 Answers3

1

Load firstly fragment in activity then set value

public class WebFragmentActivity extends AppCompatActivity implements WebListFragment.ListItemListener{

    private Fragment fragmentWebList;
    private Fragment fragmentWebContent;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_fragment);
        loadFragment(fragmentWebContent,"WebContentFragmnet");
    }

    @Override
    public void itemClick(String url) {

        ((WebContentFragment )fragmentWebContent).setUrl(url,this);
    }

    private void loadFragment(Fragment fragment,String tag){
      FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();  
        ft.replace(R.id.container, fragment, tag);
        ft.addToBackStack(tag);
        ft.commit();
    }
}
Ashish Agrawal
  • 1,977
  • 2
  • 18
  • 32
1

As i know, LayoutInflater.inflate() cant return null, it can return the view or throw an exception, but not null.

Anyway, this line:

webView = (WebView) this.getView().findViewById(R.id.webView);

should be:

webView = (WebView) view.findViewById(R.id.webView);

Your call to getView() is returning null since it give you the view returned by onCreateView()

And, it has no point with having that view class variable, Fragment class already stores its root view.

Hope this helps.

EDIT

What is null its your fragment object.

Fragment creation code from Ashish Agrawal at below answer looks like a proper solution to this.

You need to create the fragment and attach it to the activity before you can call that setUrl()

Nanoc
  • 2,381
  • 1
  • 20
  • 35
1

Fragment can't exist without the activity. You created new WebContentFragment, not that one in the WebFragmentActivity, and you didn't attach it to Activity so onCreateView wasn't called so webView is null. I assume you have WebContentFragment in your Activity layout so get fragment from the layout by calling

WebContentFragment webContentFragment = (WebContentFragment ) getSupportFragmentManager().findFragmentById(R.id.your_id);
webContentFragment .setUrl(url,this);

where your_id WebContentFragment fragment id in WebFragmentActivity layout.

HellCat2405
  • 752
  • 7
  • 16