【问题标题】:NullPointerException while trying to launch a custom DialogFragment尝试启动自定义 DialogFragment 时出现 NullPointerException
【发布时间】:2019-01-25 13:33:52
【问题描述】:

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

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

请任何人帮忙。

这是片段类:

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

【问题讨论】:

标签: android nullpointerexception android-fragmentactivity


【解决方案1】:

您没有正确使用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);

【讨论】:

  • 哦,谢谢。 IDE 要求一个方法限定符,我为什么要放置 textViews
  • 顺便说一句,对话框标题没有出现,知道吗?
  • 在设备/模拟器上测试时是否有任何日志或警告?
  • 我不是 100% 确定,但也许不是在片段中设置对话框的标题,而是应该在 gameModeSelectorFragment.show() 之前创建对话框的地方进行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-11
  • 1970-01-01
  • 2014-08-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
相关资源
最近更新 更多