java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.view.View android.view.View.findViewById(int)”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37177999/
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-08-11 19:04:48  来源:igfitidea点击:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

javaandroidandroid-fragments

提问by user4913383

I have a BlankFragment

我有一个 BlankFragment

package com.hfad.addingafragmenttoframelayout;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.w3c.dom.Text;


/**
 * A simple {@link Fragment} subclass.
 */
public class BlankFragment extends Fragment {


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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d("message","onCreateView() of BlankFragment");
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank, container, false);
    }

    public void setText(String textToBeSet){
        TextView tv = (TextView)this.getView().findViewById(R.id.fragmentText);
        tv.setText(textToBeSet);
    }

}

and it's layout is:

它的布局是:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.hfad.addingafragmenttoframelayout.BlankFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment"
        android:id="@+id/fragmentText"/>

</FrameLayout>

Here is the MainActivity

这里是 MainActivity

package com.hfad.addingafragmenttoframelayout;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //BlankFragment bf = new BlankFragment();
        //getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, bf).commit();

        BlankFragment bf = new BlankFragment();
        bf.setText("hello world");
        getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, bf).commit();


    }




}

and here is the layout of MainActivity

这是布局 MainActivity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.hfad.addingafragmenttoframelayout.MainActivity"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fragment_container"
        />


</LinearLayout>

In the onCreate method of activity I am adding fragment to layout. This works fine unless I try to change the Text using setText method.

在活动的 onCreate 方法中,我将片段添加到布局中。这工作正常,除非我尝试使用 setText 方法更改文本。

And I get this error:

我得到这个错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
                                                                                           at com.hfad.addingafragmenttoframelayout.BlankFragment.setText(BlankFragment.java:35)
                                                                                           at com.hfad.addingafragmenttoframelayout.MainActivity.onStart(MainActivity.java:29)

referring to this line

参考这一行

TextView tv = (TextView)this.getView().findViewById(R.id.fragmentText);

Can somebody explain what's causing this exception?

有人可以解释导致此异常的原因吗?

回答by PeDuCKA

TextView tv = (TextView)this.getView().findViewById(R.id.fragmentText); 

do it in onCreateView();

在 onCreateView() 中进行;

回答by ρяσ?ρ?я K

Here:

这里:

bf.setText("hello world");

line causing issue, becuase calling setTextmethod of Fragment before adding Fragmentin FragmentManager.

行造成的问题,监守调用setText前加入片段的方法FragmentFragmentManager

Call setTextmethod after adding bfFragment:

setText添加bfFragment后的调用方法:

    getSupportFragmentManager().beginTransaction().
    add(R.id.fragment_container, bf).commit();
    bf.setText("hello world"); // call here

回答by IntelliJ Amiya

Don't

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    Log.d("message","onCreateView() of BlankFragment");
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_blank, container, false);
}

public void setText(String textToBeSet){
    TextView tv = (TextView)this.getView().findViewById(R.id.fragmentText);
    tv.setText(textToBeSet);
}

Do

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

    View RootView = inflater.inflate(R.layout.fragment_blank, container, false);

    TextView tv = (TextView)RootView.findViewById(R.id.fragmentText);
    tv.setText("HI");

    return RootView;
}

回答by Enzokie

You can use a temporary variable that can hold the Stringwhile the View is not yet created.

您可以使用一个临时变量,该变量可以在String尚未创建视图时保存。

private TextView tv;
private String holder = ""; 

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

    View v = inflater.inflate(R.layout.fragment_blank, container,false);

    tv = (TextView)v.findViewById(R.id.fragmentText);
    return v;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
   tv.setText(holder);
}

public void setText(String textToBeSet){
    if(tv != null){
       tv.setText(textToBeSet);
    }else{
       holder = textToBeSet;
    }
}

回答by Cyrus Bakhtiari-Haftlang

My assumption is that you want to instantiate your BlankFragmentwith some initial text as well as allowing the text to be set later on from your MainActivity.

我的假设是您想BlankFragment用一些初始文本实例化您的文本,并允许稍后从您的MainActivity.

In that case I would first change the the Fragment to accept an initial text like so:

在这种情况下,我会首先更改 Fragment 以接受这样的初始文本:

public class BlankFragment extends Fragment {
    private static final String ARG_INITIAL_TEXT = "arg_initial_text";

    public static BlankFragment newInstance(CharSequence initialText) {
        final BlankFragment fragment = new BlankFragment();
        final Bundle args = new Bundle();
        args.putCharSequence(ARG_INITIAL_TEXT, initialText);

        fragment.setArguments(args);
        return fragment;
    }

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

    private CharSequence mInitialText;
    private TextView mText;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mInitialText = getArguments().getCharSequence(ARG_INITIAL_TEXT);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        Log.d("message", "onCreateView() of BlankFragment");
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mText = view.findViewById(R.id.fragmentText);

        mText.setText(mInitialText);
    }

    public void setText(CharSequence textToBeSet) {
        mText.setText(textToBeSet);
    }
}

The Activity code would then look like the following:

活动代码如下所示:

public class MainActivity extends AppCompatActivity {
    private BlankFragment mBlankFragment;

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

        if (savedInstanceState == null) {
            final Fragment bf = BlankFragment.newInstance("hello world");
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.fragment_container, bf, BlankFragment.class.getName())
                    .commit();
        }

        mBlankFragment = (BlankFragment) getSupportFragmentManager()
                .findFragmentByTag(BlankFragment.class.getName());
    }

    private void someTimeLaterWhenWantingToSetNewTextToFragmentFromActivity(CharSequence newText) {
        if (mBlankFragment != null) {
            mBlankFragment.setText(newText);
        }
    }
}

Note that the null-check of savedInstanceStateis to guard for Activity-recreation for instance due to configuration changes.

请注意,空检查savedInstanceState是为了保护 Activity-recreation,例如由于配置更改。

回答by cutiko

The problem is FragmentTransaction is async by default

问题是 FragmentTransaction 默认是异步的

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragment_container, BlankFragment.newInstance);
ft.disallowBackStack();
ft.commitNowAllowingStateLoss();

Then you still can't get the Fragment yet, you have to get it from the FragmentTransaction

那你还是拿不到Fragment,得从FragmentTransaction里拿

BlankFragment bf = (BlankFragment) getSupportFragmentManager().getFragmentById(R.id.fragment_container);
bf.setText("hello world");

This articlehelped me to understand this problem better

这篇文章帮助我更好地理解了这个问题