我试图在开关盒上设置一个单击侦听器,它将返回带有特定文本的自定义对话框,具体取决于具体情况。 当我运行应用程序并单击负责显示具有特定结果的对话框的按钮时,应用程序崩溃。
当我检查调试器可能出现的错误时,它会转到我尝试更改对话框文本的行。例如:
gm.setText("result6");
Java代码
private void getHint(final int viewId) {
int remainHints = Integer.parseInt(hintsValue.getText().toString());
if (isHintUsed(viewId) != 1) {
if (remainHints < 8) {
final Dialog dialog = new Dialog(TestActivity.this);
// Include dialog.xml file
dialog.setContentView(R.layout.dialog);
dialog.show();
Button declineButton = (Button) dialog.findViewById(R.id.btn_cancel);
// if decline button is clicked, close the custom dialog
declineButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
}else {
if (viewId == R.id.letter && isLetterHintOn == 1) {
stopLetterHint();
} else {
final Dialog dialog = new Dialog(TestActivity.this);
dialog.setContentView(R.layout.dialog);
switch (viewId) {
case R.id.mom:
TextView mom = (TextView) findViewById(R.id.tv_description);
mom.setText("result 1");
break;
case R.id.dad:
TextView dad = (TextView) findViewById(R.id.tv_description);
dad.setText("result 2");
break;
case R.id.brother:
TextView brother = (TextView) findViewById(R.id.tv_description);
brother.setText("result 3");
break;
case R.id.sister:
TextView sis = (TextView) findViewById(R.id.tv_description);
sis.setText("result 4");
break;
case R.id.gfather:
TextView gf = (TextView) findViewById(R.id.tv_description);
gf.setText("result 5");
break;
case R.id.gmother:
TextView gm = (TextView) findViewById(R.id.tv_description);
gm.setText("result 6");
break;
}
Button videoButton = (Button) dialog.findViewById(R.id.btn_video);
// if decline button is clicked, close the custom dialog
videoButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (viewId != R.id.letter) {
db.addUsedHint();
hintsValue.setText(String.valueOf(getHintsNumber()));
}
executeHint(viewId);
}
});
Button declineButton = (Button) dialog.findViewById(R.id.btn_cancel);
// if decline button is clicked, close the custom dialog
declineButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
dialog.show();
}
}
} else {
executeHint(viewId);
}
}
xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<RelativeLayout
android:id="@+id/rl_quit_learning"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="@drawable/popup"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp">
<TextView
android:id="@+id/tv_quit_learning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:textColor="#000"
android:text="Quit LEarning?"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_quit_learning"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:textColor="#000"
android:gravity="center"
android:text="You are 400pts. away from \n unlocking rewards. Quit LEarning?"
android:textSize="16sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="125dp"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/btn_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/lock" />
<Button
android:id="@+id/btn_video"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/lock" />
</LinearLayout>
logcat的
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.momdad.family, PID: 18297
java.lang.NullPointerException: Attempt to invoke virtual
method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.momdad.family
.TestActivity.getHint(TestActivity.java:1171)
at com.momdad.family
.TestActivity.access$200(TestActivity.java:67)
at com.momdad.family
.TestActivity$12.onClick(TestActivity.java:1059)
at android.view.View.performClick(View.java:5702)
at
android.view.View$PerformClick.run(View.java:22533)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
答案 0 :(得分:0)
使用以下代码替换您的代码:
switch (viewId) {
case R.id.mom:
TextView mom = (TextView) dialog.findViewById(R.id.tv_description);
mom.setText("result 1");
break;
case R.id.dad:
TextView dad = (TextView) dialog.findViewById(R.id.tv_description);
dad.setText("result 2");
break;
case R.id.brother:
TextView brother = (TextView) dialog.findViewById(R.id.tv_description);
brother.setText("result 3");
break;
case R.id.sister:
TextView sis = (TextView) dialog.findViewById(R.id.tv_description);
sis.setText("result 4");
break;
case R.id.gfather:
TextView gf = (TextView) dialog.findViewById(R.id.tv_description);
gf.setText("result 5");
break;
case R.id.gmother:
TextView gm = (TextView) dialog.findViewById(R.id.tv_description);
gm.setText("result 6");
break;
}