[英]Why my android app is going back to previous activity while I fill up a simple question form to database?
I am trying to build a quiz app, where admin will create questions in admin activity and these questions will be shown as quiz exam in another activity.我正在尝试构建一个测验应用程序,管理员将在管理活动中创建问题,这些问题将在另一个活动中显示为测验考试。 While I try to fill up the form by clicking CREATE QUESTION button from admin activity my app is going back to previous activity(adminlogin activity) Here is the code of AdminActivity:
当我尝试通过单击管理活动中的 CREATE QUESTION 按钮来填写表单时,我的应用程序将返回到之前的活动(adminlogin 活动)以下是 AdminActivity 的代码:
public class AdminActivity extends AppCompatActivity {
Button btnCreateQuestion, btnReadQuestion, btnDeleteQuestion;
EditText editTextQuestion, editTextOption1, editTextOption2, editTextOption3, editTextOption4,
editTextAnswerNo,editTextChapterNo;
QuizDbHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
btnCreateQuestion = findViewById(R.id.btnCreateQuestion);
btnReadQuestion = findViewById(R.id.btnReadQuestions);
btnDeleteQuestion = findViewById(R.id.btnDeleteQuestions);
editTextQuestion = findViewById(R.id.editTextQuestion);
editTextOption1 = findViewById(R.id.editTextOption1);
editTextOption2 = findViewById(R.id.editTextOption2);
editTextOption3 = findViewById(R.id.editTextOption3);
editTextOption4 = findViewById(R.id.editTextOption4);
editTextAnswerNo = findViewById(R.id.editTextAnswerNo);
editTextChapterNo = findViewById(R.id.editTextChapterNo);
db = new QuizDbHelper(this);
btnCreateQuestion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String question = editTextQuestion.getText().toString();
String option1 = editTextOption1.getText().toString();
String option2 = editTextOption2.getText().toString();
String option3 = editTextOption3.getText().toString();
String option4 = editTextOption4.getText().toString();
Integer answerNo = Integer.parseInt(editTextAnswerNo.getText().toString());
String chapterName = editTextAnswerNo.getText().toString();
if( question != "" && option1 != "" && option2 != "" && option3 != "" && option4 != "" && answerNo!= null && chapterName != ""){
Question q = new Question(question,option1,option2,option3,option4,answerNo,chapterName);
db.addQuestion(q);
Toast.makeText(AdminActivity.this,"Question Added", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(AdminActivity.this,"Please fill up", Toast.LENGTH_SHORT).show();
}
}
});
btnReadQuestion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ArrayList<Question> allQuestions = db.getAllQuestions();
if(allQuestions.size()< 1){
Toast.makeText(AdminActivity.this,"No questions in the Database", Toast.LENGTH_SHORT).show();
}else {
StringBuffer buffer = new StringBuffer();
for (Question q: allQuestions) {
buffer.append("Question : " + q.getQuestion() + "\n" );
buffer.append("Option1: " + q.getOption1() + "\n");
buffer.append("Option2: " + q.getOption2() + "\n");
buffer.append("Option3: " + q.getOption3() + "\n");
buffer.append("Option4: " + q.getOption4() + "\n");
buffer.append("Answer No: " + q.getAnswerNo() + "\n");
buffer.append("Option4: " + q.getChapterName() + "\n\n");
}
AlertDialog.Builder builder = new AlertDialog.Builder(AdminActivity.this);
builder.setCancelable(true);
builder.setTitle("Questions");
builder.setMessage(buffer.toString());
builder.show();
}
}
});
btnDeleteQuestion.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}}
Any clue?有什么线索吗? Thank You in Advance.
先感谢您。
Check in onCreate
function of your Activity class, did you have initialize your EditText
and Button
.检查您的 Activity 类的
onCreate
函数,您是否已初始化您的EditText
和Button
。
Also, You have to make change in your condition as below:此外,您必须改变您的状况,如下所示:
if(!question.equalsIgnoreCase("") && !option1.equalsIgnoreCase("") && !option2.equalsIgnoreCase("") && !option3.equalsIgnoreCase("") && !option4.equalsIgnoreCase("") && answerNo != 0 && !chapterName.equalsIgnoreCase("")
Because, ==
tests for reference equality (whether they are the same object).因为,
==
测试引用相等性(它们是否是同一个对象)。
Whereas, equalsIgnoreCase()
ignores the case while comparing two strings.而
equalsIgnoreCase()
在比较两个字符串时忽略大小写。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.