Я создал деятельность с двумя кнопками, в которых диалог пользовательских окно будет показано в зависимости от типа кнопки нажалNPE в диалоговом окне пользовательского
Я создал файл XML для диалогового окна диалога
.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>
THe активность основной является
<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)
, пожалуйста, помогите!
, что я s lv_dailog вы хотите загрузить этот диалог.xml в lv_dailog? –