我创建了一个带有两个按钮的活动,根据点击的按钮类型,将显示一个自定义对话框
我为对话框
创建了一个xml文件dialog.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/lv_dialog"
android:background="#FF0000"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="# Do you know?"
android:layout_gravity="center"
android:textSize="25px"
android:layout_marginTop="10px"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="THE FACT IS DISPLAYED HERE"
android:layout_gravity="center"
android:textSize="30px"
android:layout_marginTop="10px"
android:id="@+id/txtfact"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="THE ANSWER STATUS IS DISPLAYED HERE"
android:layout_gravity="center"
android:textSize="32px"
android:textColor="#FFFFFF"
android:layout_marginTop="10px"
android:id="@+id/txtanswer"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="THE SCORE IS DISPLAYED HERE"
android:layout_gravity="center"
android:textSize="32px"
android:textColor="#FFFFFF"
android:layout_marginTop="10px"
android:id="@+id/txtscore"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GOTO NEXT QUESTION"
android:layout_gravity="center"
android:textColor="#FFFFFF"
android:layout_marginTop="20px"
android:id="@+id/btngotonext"
/>
</LinearLayout>
活动主要是
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CORRECT CHOICE"
android:id="@+id/btncorrect"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WRONG CHOICE"
android:id="@+id/btnwrong"
/>
</LinearLayout>
主要课程是:
package com.example.custom_fifa_dialog;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button btncorrect,btnwrong,btngotonext;
LinearLayout lv_dialog;
Dialog dialog;
databasemanager dbm;
String s="";
TextView txtfact,txtanswer,txtscore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbm=new databasemanager(this);
dialog=new Dialog(this);
btncorrect=(Button) findViewById(R.id.btncorrect);
btnwrong=(Button) findViewById(R.id.btnwrong);
lv_dialog=(LinearLayout) findViewById(R.id.lv_dialog);
btncorrect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
lv_dialog.setBackgroundColor(Color.parseColor("#008000"));
dialog.setContentView(R.layout.dialog);
txtfact=(TextView) dialog.findViewById(R.id.txtfact);
txtanswer=(TextView) dialog.findViewById(R.id.txtanswer);
txtscore=(TextView) dialog.findViewById(R.id.txtscore);
btngotonext=(Button) dialog.findViewById(R.id.btngotonext);
int random=1 + (int)(Math.random() * ((736 -1) + 1));
dialog.setTitle("RESULT");
Toast.makeText(getApplicationContext(), ""+random,Toast.LENGTH_SHORT).show();
s=dbm.getdouknow(random);
txtfact.setText(s);
txtanswer.setText("CORRECT ANSWER");
txtscore.setText("SCORE:25");
btngotonext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.setCancelable(false);
dialog.show();
}
});
btnwrong.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
lv_dialog.setBackgroundColor(Color.parseColor("#FF0000"));
dialog.setContentView(R.layout.dialog);
txtfact=(TextView) dialog.findViewById(R.id.txtfact);
txtanswer=(TextView) dialog.findViewById(R.id.txtanswer);
txtscore=(TextView) dialog.findViewById(R.id.txtscore);
btngotonext=(Button) dialog.findViewById(R.id.btngotonext);
int random=1 + (int)(Math.random() * ((736 -1) + 1));
dialog.setTitle("RESULT");
Toast.makeText(getApplicationContext(), ""+random,Toast.LENGTH_SHORT).show();
s=dbm.getdouknow(random);
txtfact.setText(s);
txtanswer.setText("WRONG ANSWER");
txtscore.setText("SCORE:0");
btngotonext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.setCancelable(false);
dialog.show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
我在设置背景颜色的行处获得NPE:
logcat的:
06-09 10:45:00.660: E/AndroidRuntime(18253): FATAL EXCEPTION: main
06-09 10:45:00.660: E/AndroidRuntime(18253): Process: com.example.custom_fifa_dialog, PID: 18253
06-09 10:45:00.660: E/AndroidRuntime(18253): java.lang.NullPointerException
06-09 10:45:00.660: E/AndroidRuntime(18253): at com.example.custom_fifa_dialog.MainActivity$1.onClick(MainActivity.java:38)
06-09 10:45:00.660: E/AndroidRuntime(18253): at android.view.View.performClick(View.java:4438)
06-09 10:45:00.660: E/AndroidRuntime(18253): at android.view.View$PerformClick.run(View.java:18422)
06-09 10:45:00.660: E/AndroidRuntime(18253): at android.os.Handler.handleCallback(Handler.java:733)
06-09 10:45:00.660: E/AndroidRuntime(18253): at android.os.Handler.dispatchMessage(Handler.java:95)
06-09 10:45:00.660: E/AndroidRuntime(18253): at android.os.Looper.loop(Looper.java:136)
06-09 10:45:00.660: E/AndroidRuntime(18253): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-09 10:45:00.660: E/AndroidRuntime(18253): at java.lang.reflect.Method.invokeNative(Native Method)
06-09 10:45:00.660: E/AndroidRuntime(18253): at java.lang.reflect.Method.invoke(Method.java:515)
06-09 10:45:00.660: E/AndroidRuntime(18253): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-09 10:45:00.660: E/AndroidRuntime(18253): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-09 10:45:00.660: E/AndroidRuntime(18253): at dalvik.system.NativeStart.main(Native Method)
请帮忙!
答案 0 :(得分:1)
当您致电lv_dialog=(LinearLayout) findViewById(R.id.lv_dialog);
时,您正在当前活动上调用findViewById()
,而不是对话框。由于具有标识lv_dialog
的LinearLayout位于Dialog的布局中而不是Activity,因此将返回null。
在致电dialog.setContentView(R.layout.dialog);
之前,您的对话框布局甚至不会膨胀。
解决这个问题的一种方法是在设置Dialog的布局之前不检索LinearLayout,如下所示:
btncorrect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.setContentView(R.layout.dialog);
lv_dialog=(LinearLayout) dialog.findViewById(R.id.lv_dialog);
lv_dialog.setBackgroundColor(Color.parseColor("#008000"));
// ...
}
}
答案 1 :(得分:0)
您需要在setContent for dialog后调用此行。在开始时,您将获得空引用。
lv_dialog=(LinearLayout) findViewById(R.id.lv_dialog);
答案 2 :(得分:0)
在您的活动lv_dialog
中,dialog.xml
文件不属于activity_main
。
所以
设置对话框布局后,将lv_dialog
的实例设为
改变这个
lv_dialog.setBackgroundColor(Color.parseColor("#008000"));
dialog.setContentView(R.layout.dialog);
到
dialog.setContentView(R.layout.dialog);
lv_dialog=(LinearLayout) dialog.findViewById(R.id.lv_dialog);
lv_dialog.setBackgroundColor(Color.parseColor("#008000"));
并删除
lv_dialog=(LinearLayout) findViewById(R.id.lv_dialog);
来自OnCreate
函数