java Android 片段和空对象引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33469159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 21:43:03  来源:igfitidea点击:

Android fragment and null object reference

javaandroidandroid-fragments

提问by AlwaysConfused

I am trying to get my fragment working and I cant get it done whatever I am trying to do.

我正在努力让我的片段工作,但无论我想做什么,我都无法完成。

The error I am getting is:

我得到的错误是:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)”

Here is the code:

这是代码:

public class FragmentOne extends Fragment {

    private TextView one;

    public FragmentOne() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        one = (TextView) getActivity().findViewById(R.id.one);

        // Displaying the user details on the screen
        one.setText("kjhbguhjg");

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment1, container, false);

    }
}

Have no idea why it doesn't work. I am testing this class just to see if the text will be changed on the textview. I am using the right id because I checked that like 10 times, yet I think the problem is because textview one is a null object. But why it doesn't find the id?

不知道为什么它不起作用。我正在测试这个类只是为了查看文本视图上的文本是否会更改。我使用了正确的 id,因为我检查了 10 次,但我认为问题是因为 textview one 是一个空对象。但是为什么找不到id呢?

回答by Daniel Olsson

onCreate()is called before onCreateView()and therefore you will not be able to access it in onCreate().

onCreate()之前被调用onCreateView(),因此您将无法在onCreate().

Solution:Move

解决方案:移动

one = (TextView) getActivity().findViewById(R.id.one);

to onViewCreated()instead.

onViewCreated()代替。

See picture below for an overview of the fragment lifecycle.

有关片段生命周期的概述,请参见下图。

The new snippet looks like this:

新代码段如下所示:

public class FragmentOne extends Fragment {


    private TextView one;

    public FragmentOne() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment1, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState){
        one = (TextView) getActivity().findViewById(R.id.one);
        // Displaying the user details on the screen
        one.setText("kjhbguhjg");
    }
}

Fragment lifecycle

片段生命周期

回答by JDRussia

Or. instead of rewriting

或者。而不是重写

public void onViewCreated(View view, Bundle savedInstanceState)

you can slightly change your onCreateView so it looks like

你可以稍微改变你的 onCreateView 所以它看起来像

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment1, container, false)
one = (TextView) rootView.findViewById(R.id.one)
return rootView;
}