在片段中使用getView().findViewById返回NullPointerException [英] Using getView().findViewById in fragment returns NullPointerException

查看:103
本文介绍了在片段中使用getView().findViewById返回NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它的确给了我警告,但我从未使用过碎片,所以我不确定如何修复它.我在尝试查找工具栏的代码的第三行出现错误.

It does give me warning but I've never used fragments so I'm not sure how to fix it. I get the error on the third line of the code where I'm trying to find the toolbar.

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = (View) inflater.inflate(R.layout.fragment_contact, container, false);

        Toolbar toolbar = getView().findViewById(R.id.toolbar);
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(false);

        TextView textView = getView().findViewById(R.id.vive_unadeca);
        textView.setOnClickListener(new View.OnClickListener() {//do stuff here} });

        LinearLayout mapsT = getView().findViewById(R.id.locationT);
        ImageView mapsI = getView().findViewById(R.id.location);

        mapsT.setOnClickListener(new View.OnClickListener() {//do stuff here} });

        mapsI.setOnClickListener(new View.OnClickListener() {//do stuff here} });

        LinearLayout phoneT = getView().findViewById(R.id.phone_num);
        ImageView phoneI = getView().findViewById(R.id.phone_icon);

        phoneT.setOnClickListener(new View.OnClickListener() {//do stuff here} });

        phoneI.setOnClickListener(new View.OnClickListener() {//do stuff here} });

        LinearLayout emailT = getView().findViewById(R.id.email);
        ImageView emailI = getView().findViewById(R.id.email_icon);

        emailT.setOnClickListener(new View.OnClickListener() {//do stuff here} });

        emailI.setOnClickListener(new View.OnClickListener() {//do stuff here} });

        LinearLayout whatsappT = getView().findViewById(R.id.whatsapp_num);
        ImageView whatsappI = getView().findViewById(R.id.whatsapp_icon);

        whatsappT.setOnClickListener(new View.OnClickListener() {//do stuff here} });

        whatsappI.setOnClickListener(new View.OnClickListener(){//do stuff here} });

        return view;
    }

在clickListener中,我调用了onCreateView下面的其他方法,这些方法只调用意图,例如打开gmail,打开whatsapp,打开电话,Google地图和Facebook.

In the clickListeners I call other methods that are underneath the onCreateView which just call the intents like open gmail, open whatsapp, open phone call, google maps and facebook.

我在做什么错了?

推荐答案

您正在 onCreateView()中调用 getView().到那时,尚未设置 view ,因为您正确地构造了它.

You're calling getView() inside onCreateView(). By that time, the view is not set, since you're right then constructing it.

getView()的调用将在 onCreateView()之后和 onDestroyView()之前返回非null的>.

The call to getView() will return non-null after onCreateView() and before onDestroyView().

相反,只需在刚刚膨胀的 View 中调用 view.findViewById ,我认为这就是您想要的.

Instead, just call view.findViewById in the View you have just inflated, which I assume is what you want.

LinearLayout mapsT = view.findViewById(R.id.locationT);

这篇关于在片段中使用getView().findViewById返回NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆