在创建对话框视图的过程中,出现此错误:
FATAL EXCEPTION: main
Process: com.example.myapp, PID: 26135
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:825)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:828)
at android.view.LayoutInflater.inflate(LayoutInflater.java:523)
at android.view.LayoutInflater.inflate(LayoutInflater.java:425)
at android.view.LayoutInflater.inflate(LayoutInflater.java:368)
at com.example.myapp.NewGameActivity.createTeam(NewGameActivity.java:145)
at com.example.myapp.NewGameActivity.onItemSelected(NewGameActivity.java:394)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:917)
at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:906)
at android.widget.AdapterView.access$300(AdapterView.java:51)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:876)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5593)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
这是导致异常的代码:
final View dialogView = inflater.inflate(R.layout.dialog_layout_add_team, rootView);
更多上下文代码:
//Dialog
// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(NewGameActivity.this);
LayoutInflater inflater = getLayoutInflater();
ViewGroup rootView = findViewById(R.id.root_new_game);
final View dialogView = inflater.inflate(R.layout.dialog_layout_add_team, rootView);
// 2. Chain together various setter methods to set the dialog characteristics
builder
.setTitle(R.string.new_team)
.setView(dialogView)
.setPositiveButton(R.string.done, null)
.setCancelable(false)
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
// User cancelled the dialog
}
});
// 3. Get the AlertDialog from create()
final AlertDialog dialog = builder.create();
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// User clicked OK button
//Dismiss once everything is OK.
dialog.dismiss();
}
}
});
}
});
dialog.show();
我也尝试使用null(导致相同的错误):
final View dialogView = inflater.inflate(R.layout.dialog_layout_add_team, null);
添加attachToRoot值不会更改错误:
final View dialogView = inflater.inflate(R.layout.dialog_layout_add_team, null, false);
更新:dialog_layout_add_team.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root_add_team_dialog_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/enter_a_team_title"
android:textColor="@color/primaryTextColor" />
<EditText
android:id="@+id/team_title_edit_text_add_team"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/team_title" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/add_players"
android:textColor="@color/primaryTextColor" />
<TextView
android:id="@+id/current_players_text_view_add_team"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/current_players" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/player_number_edit_text_add_team"
android:layout_weight="80"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/player_number"
android:inputType="number" />
<view
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="20"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/player_name_edit_text_add_team"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="80"
android:hint="@string/player_name" />
<ImageButton
android:id="@+id/add_player_imageButton_add_team"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
app:srcCompat="@drawable/ic_person_add_white_24dp" />
</LinearLayout>
感谢您的帮助!