[英]NPE at custom dialog box
我用兩個按鈕創建了一個活動,其中將根據單擊的按鈕類型顯示自定義對話框
我已經為對話框創建了一個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)
請幫忙!
當您調用lv_dialog=(LinearLayout) findViewById(R.id.lv_dialog);
,您是在當前Activity而不是對話框上調用findViewById()
。 由於ID為lv_dialog
的LinearLayout位於對話框的布局中,而不位於Activity中,因此將返回null。
直到調用dialog.setContentView(R.layout.dialog);
對話框的布局才會膨脹dialog.setContentView(R.layout.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"));
// ...
}
}
在為對話框設置內容之后,需要調用此行。 一開始,您將獲得空引用。
lv_dialog=(LinearLayout) findViewById(R.id.lv_dialog);
在您的活動中lv_dialog
是dialog.xml
文件的一部分,而不是activity_main
一部分。 因此,在將對話框布局設置為“ Change this”后, 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
函數
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.