I got a weird problem with my method that's creating dialoges with questions. Basically what I've done is creating a hashmap table with questions and a identifier, which I'm using for my if-statements on the positive -and negative buttons of the dialog window.
Here's the program code:
public class QuestionsActivity extends Activity {
String question = null;
String identifier = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
// 0 = call showRiskEvaluationDialog() on button NO. 1 = call showRiskEvaluationDialog(); on button YES
Map<String, String> question_map = new HashMap<String, String>();
question_map.put("Have you cut the power?", "0");
question_map.put("Have you not cut the power?", "1");
question_map.put("Do you know your work assignment?", "0");
question_map.put("Don't you know your work assignment?", "1");
//For loop to extract a question and identifier from the hashmap
for(Entry<String, String> entry : question_map.entrySet())
{
question = entry.getKey();
identifier = entry.getValue();
create_dialog_method(question);
}
}
/**
* Method to create a dialogbox with a question and YES, NO buttons
* @param question_param
*/
public void create_dialog_method(String question_param) {
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Titel");
alert.setMessage(question_param);
alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if (identifier.equalsIgnoreCase("1")) {
showRiskEvaluationDialog();
identifier = null;
} else if (identifier.equalsIgnoreCase("0")) {
Toast.makeText(getApplicationContext(), "Saved",
Toast.LENGTH_SHORT).show();
identifier = null;
}
}
});
alert.setNegativeButton("NO", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int whichButton) {
if (identifier.equalsIgnoreCase("0")) {
showRiskEvaluationDialog();
identifier = null;
} else if (identifier.equalsIgnoreCase("1")) {
Toast.makeText(getApplicationContext(), "Saved",
Toast.LENGTH_SHORT).show();
identifier = null;
}
}
});
alert.show();
}
PROBLEM: First of all it seems to loop through the hashmap in a weird order. It's starting with "Have you not cut the power?" and therefor skipping the first question. The main problem when I run the program is that the application crash after I've answered the 2:nd dialog popup. Basically it runs through 1 question perfectly but when I'm answering the 2:nd question it krasches, no matter what button I reply with.
Here's the errors:
04-26 14:13:35.163: E/AndroidRuntime(7953): FATAL EXCEPTION: main
04-26 14:13:35.163: E/AndroidRuntime(7953): java.lang.NullPointerException
04-26 14:13:35.163: E/AndroidRuntime(7953): at com.example.ovakoappen.QuestionsActivity$2.onClick(QuestionsActivity.java:85)
04-26 14:13:35.163: E/AndroidRuntime(7953): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:185)
04-26 14:13:35.163: E/AndroidRuntime(7953): at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 14:13:35.163: E/AndroidRuntime(7953): at android.os.Looper.loop(Looper.java:137)
04-26 14:13:35.163: E/AndroidRuntime(7953): at android.app.ActivityThread.main(ActivityThread.java:5279)
04-26 14:13:35.163: E/AndroidRuntime(7953): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 14:13:35.163: E/AndroidRuntime(7953): at java.lang.reflect.Method.invoke(Method.java:511)
04-26 14:13:35.163: E/AndroidRuntime(7953): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
04-26 14:13:35.163: E/AndroidRuntime(7953): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
04-26 14:13:35.163: E/AndroidRuntime(7953): at dalvik.system.NativeStart.main(Native Method)