尝试启动自定义 DialogFragment 时出现 NullPointerException

NullPointerException while trying to launch a custom DialogFragment

我正在尝试从 activity 启动具有 4 个 TextView 的自定义 DialogFragment,但应用程序在启动时崩溃。

stactrace 显示来自那些我找不到原因的 TextView 的 NullPointerException。

请大家帮忙。

这是片段class:

    public class MyCustomragment extends DialogFragment {

    private TextView mTv1, mTv2,mTv3,mTv4;

    public GameModeSelectorFragment(){

    }

    public static GameModeSelectorFragment newInstance(String title){
        GameModeSelectorFragment gmsf = new GameModeSelectorFragment();
        Bundle args = new Bundle();
        args.putString("title", title);
        gmsf.setArguments(args);
        return gmsf;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        return inflater.inflate(R.layout.fragment_game_mode_selector, container);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState){
        super.onViewCreated(view, savedInstanceState);

        //THE BELOW ARE THROWING THE EXCEPTION
        mTv1 = mTv1.findViewById(R.id.tv_1);
        mTv2 = mTv2.findViewById(R.id.tv_2);
        mTv3 = mTv3.findViewById(R.id.tv_3);
        mTv4 = mTv4.findViewById(R.id.tv_4);

        String title = getArguments().getString("title", String.valueOf(R.string.mode_selector_frag_title));
        getDialog().setTitle(title);

    }

}

XML布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/ll_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

       <TextView
            android:id="@+id/tv_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/textView1"/>


        <TextView
            android:id="@+id/tv_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/textView2"/>


        <TextView
            android:id="@+id/tv_3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/textView3"/>


        <TextView
            android:id="@+id/tv_4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/textView4"/>

    </LinearLayout>

我从 activity 的 onCreate 方法调用片段 onTouch 方法:

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

//...

 button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showModeSelectionDialog();
            }
        });

}

private void showModeSelectionDialog() {
        FragmentManager fragmentManager = getSupportFragmentManager();
        GameModeSelectorFragment gameModeSelectorFragment = GameModeSelectorFragment.newInstance("Titre");
        gameModeSelectorFragment.show(fragmentManager, "fragment_game_mode_selector");
    }

您没有正确使用 findViewById 方法。如果不设置 mTv1 或任何其他视图,则无法调用它们的任何方法。这就是为什么你得到 NullPointerException

这是正确的版本:

mTv1 = view.findViewById(R.id.tv_1);
mTv2 = view.findViewById(R.id.tv_2);
mTv3 = view.findViewById(R.id.tv_3);
mTv4 = view.findViewById(R.id.tv_4);