我正在尝试从一个活动中启动具有4个TextViews的自定义DialogFragment,但是该应用在启动时崩溃。
stactrace从无法找到原因的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>
我从活动的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");
}
答案 0 :(得分:0)
您没有正确使用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);