片段通信抛出Null指针异常

时间:2015-11-04 10:22:50

标签: android android-fragments

我是关于在父活动中以两个片段进行通信的演示。这是我的代码

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);
    }
}

WebContentFragment 中调用方法 setUrl 时,我遇到的问题会导致LogCat中出现空指针异常。当我在这个方法中调试我看到视图和webView等于null 虽然我分配了 onCreateView 。我不知道为什么会这样。你能告诉我这个问题的原因和解决方法吗?谢谢

我的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 个答案:

答案 0 :(得分:1)

首先在活动中加载片段然后设置值

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();
    }
}

答案 1 :(得分:1)

据我所知,LayoutInflater.inflate()无法返回null,它可以返回视图或抛出异常,但不是null。

无论如何,这一行:

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

应该是:

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

你对getView()的调用返回null,因为它为你提供了onCreateView()返回的视图

并且,拥有该视图类变量没有意义,Fragment类已经存储了它的根视图。

希望这有帮助。

修改

你的片段对象是什么null。

来自Ashish Agrawal的片段创建代码在下面的回答看起来像是一个正确的解决方案。

您需要创建片段并将其附加到活动,然后才能调用该setUrl()

答案 2 :(得分:1)

片段在没有活动的情况下无法存在。您创建了 new WebContentFragment,而不是WebFragmentActivity中的那个,并且您没有将它附加到Activity,因此onCreateView没有被调用,因此webView为null。我假设您在Activity布局中有WebContentFragment,因此通过调用

从布局中获取片段
WebContentFragment webContentFragment = (WebContentFragment ) getSupportFragmentManager().findFragmentById(R.id.your_id);
webContentFragment .setUrl(url,this);

其中your_id WebConragmentActivity布局中的WebContentFragment片段ID。