我正在使用一个在线测验考试系统,它将是一个Android应用程序。但是,当我尝试在listView中显示问题数据时,它在Android版本4.4.2中崩溃。但是在较高的android版本(例如8或9)中,它可以正常工作。 下面是我的所有代码:
activity_quiz_exam_.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#EEE4E4"
tools:context=".Activity.Quiz_Exam_Activity">
<LinearLayout
android:background="#E9C6C6"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/topicNameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="15sp"
android:layout_gravity="left"
android:padding="4dp"
android:textColor="#180056"
android:textStyle="bold"
/>
</LinearLayout>
<LinearLayout
android:layout_weight="1.2"
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content">
<TextView
android:gravity="right"
android:id="@+id/remainingTimeTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Total Time: 10:00 min"
android:textSize="15sp"
android:padding="4dp"
android:textColor="#180056"
android:textStyle="bold"
/>
</LinearLayout>
</LinearLayout>
<ProgressBar
android:layout_gravity="center_horizontal"
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="gone" />
<ListView
android:padding="2dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:cacheColorHint="#f4f400"
android:listSelector="#f95cd1"
>
</ListView>
</LinearLayout>
测验活动:
package com.example.sajib.smarticteducator.Activity;
import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.example.sajib.smarticteducator.Custom_Adapter_For_Quiz_Exam;
import com.example.sajib.smarticteducator.R;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.Random;
public class Quiz_Exam_Activity extends AppCompatActivity{
// Button btn = new Button(this);
public static String[] questionAns=new String[30];
ProgressBar progressBar;
///////
String[] Ques=new String[100];
String[] Answer=new String[100];
String[] OptionA=new String[100];
String[] OptionB=new String[100];
String[] OptionC=new String[100];
String[] OptionD=new String[100];
String Qname,join;
TextView remainingTimeTV,topicNameTV;
int[] a=new int[30];
ListView listView;
public static int size;
int i,j,temp=0,ck=0,Max=1,Marks=0,k=0,count=0;
DatabaseReference myRef1,myRef2,ref,ref2,userRef;
String value,A,D,marks,max,username;
String topicName;
String topicType;
int upperBound,lowerBound;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz__exam_);
this.setTitle("");
Intent intent=getIntent();
topicName = intent.getStringExtra("topicName");
topicType = intent.getStringExtra("topicType");
Toast.makeText(this,"Topic: "+topicName+". Type: "+topicType,Toast.LENGTH_LONG).show();
ref = FirebaseDatabase.getInstance().getReference("Quiz").child(topicName);
userRef = FirebaseDatabase.getInstance().getReference("Users");
listView=findViewById(R.id.listView);
remainingTimeTV=findViewById(R.id.remainingTimeTV);
topicNameTV=findViewById(R.id.topicNameTV);
/// progressbar start
progressBar=findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE);
topicNameTV.setText(topicName);
getMaxAndMarks(ref);
}
public void getMaxAndMarks(DatabaseReference ref){
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
max=dataSnapshot.child("Max").getValue(String.class);
marks=dataSnapshot.child("Marks").getValue(String.class);
Marks=Integer.valueOf(marks);
Max=Integer.valueOf(max);
if(topicType.equals("Easy")){
lowerBound=1;
upperBound=Marks+5;
}
else if(topicType.equals("Medium")){
lowerBound=Marks;
upperBound=Max;
}
else if(topicType.equals("Hard")){
lowerBound=Marks+6;
upperBound=Max;
}
get_And_Display_AllData();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
public void get_And_Display_AllData() {
// Toast.makeText(Quiz_Exam_Activity.this,"get_And_Display_AllData: Marks="+Marks,Toast.LENGTH_SHORT).show();
ck=0;
count=0;
// Random number store
Random random = new Random();
for(i=0;i<Marks;i++)
{
temp = random.nextInt(upperBound - lowerBound+1) + lowerBound;
for(j=0;j<i;j++)
{
if(temp==a[j])
{
ck=1;
break;
}
}
if(ck==1)
{
i--;
}
else
{
a[i]=temp;
count=count+1;
}
ck=0;
}
k=0;
getUserName();
}
public void getUserName()
{
DatabaseReference userRef = FirebaseDatabase.getInstance().getReference("Users")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid().toString());
// Toast.makeText(Quiz_Exam_Activity.this,"GetReference Function: ",Toast.LENGTH_LONG).show();
userRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
username=dataSnapshot.child("name").getValue(String.class);
getQuestionData();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
// Toast.makeText(Quiz_Exam_Activity.this,"PK: "+pk,Toast.LENGTH_LONG).show();
}
public void getMessage(DatabaseReference ref){
ref.addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot dataSnapshot) {
Ques[k]=dataSnapshot.child("Question").getValue(String.class);
Answer[k]=dataSnapshot.child("Ans").getValue(String.class);
OptionA[k]=dataSnapshot.child("A").getValue(String.class);
OptionB[k]=dataSnapshot.child("B").getValue(String.class);
OptionC[k]=dataSnapshot.child("C").getValue(String.class);
OptionD[k]=dataSnapshot.child("D").getValue(String.class);
if (Ques[k] == null)
{
get_And_Display_AllData();
}
else
{
k++;
getQuestionData();
}
}
public void onCancelled(DatabaseError error) {
// TODO Auto-generated method stub
}
});
}
public void getQuestionData()
{
if(k<Marks)
{
//Qname=getQuestionName(a[k]);
Qname="Q"+String.valueOf(a[k]);
ref2 =ref.child(Qname);
// join=join+" "+Qname;
// Toast.makeText(Quiz_Exam_Activity.this,""+Qname,Toast.LENGTH_SHORT).show();
// testTV.setText(join);
getMessage(ref2);
}
else
callListViewAdapter();
}
public void callListViewAdapter()
{
String[] Ques=new String[Marks];
String[] Answer=new String[Marks];
String[] OptionA=new String[Marks];
String[] OptionB=new String[Marks];
String[] OptionC=new String[Marks];
String[] OptionD=new String[Marks];
for(i=0;i<Marks;i++)
{
Ques[i]=this.Ques[i];
Answer[i]=this.Answer[i];
OptionA[i]=this.OptionA[i];
OptionB[i]=this.OptionB[i];
OptionC[i]=this.OptionC[i];
OptionD[i]=this.OptionD[i];
}
Custom_Adapter_For_Quiz_Exam adapter=new Custom_Adapter_For_Quiz_Exam(Quiz_Exam_Activity.this,Ques,OptionA,OptionB,OptionC,OptionD);
LayoutInflater inflater=this.getLayoutInflater();
LinearLayout listFooterView = (LinearLayout)inflater.inflate(
R.layout.footer_button_of_listview, null);
listView.addFooterView(listFooterView);
progressBar.setVisibility(View.GONE);
Button forward = listFooterView.findViewById(R.id.submitButton);
forward.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
SubmitAllAnswers();
}
});
///////////
listView.setAdapter(adapter);
countDownTimer();
}
public void countDownTimer(){
new CountDownTimer(600000, 1000){
@Override
public void onTick(long millisUntilFinished) {
long min,second;
min=millisUntilFinished/(1000*60);
second=millisUntilFinished%(1000*60);
second=second/1000;
remainingTimeTV.setText("Remaining Time: 0"+min+":"+second+" min");
}
@Override
public void onFinish() {
SubmitAllAnswers();
}
}.start();
}
public void SubmitAllAnswers( ) {
String[] Answer=new String[Marks];
String[] givenAnswer=new String[Marks];
String[] OptionA=new String[Marks];
String[] OptionB=new String[Marks];
String[] OptionC=new String[Marks];
String[] OptionD=new String[Marks];
String[] Question=new String[Marks];
String test ="";
// Toast.makeText(Quiz_Exam_Activity.this,"callListViewAdapter: K="+k,Toast.LENGTH_SHORT).show();
for(i=0;i<Marks;i++)
{
if(questionAns[i]==null)
{
givenAnswer[i]="NA";
}
else
{
givenAnswer[i]=this.questionAns[i];
}
Answer[i]=this.Answer[i];
OptionA[i]=this.OptionA[i];
OptionB[i]=this.OptionB[i];
OptionC[i]=this.OptionC[i];
OptionD[i]=this.OptionD[i];
OptionD[i]=this.OptionD[i];
Question[i]=this.Ques[i];
}
Intent intent = new Intent(this, Accuracy_And_Solve_Sheet_Activity.class);
intent.putExtra("question", Question);
intent.putExtra("givenAnswer", givenAnswer);
intent.putExtra("Answer", Answer);
intent.putExtra("OptionA", OptionA);
intent.putExtra("OptionB", OptionB);
intent.putExtra("OptionC", OptionC);
intent.putExtra("OptionD", OptionD);
intent.putExtra("Marks", Marks);
intent.putExtra("Username", username);
finish();
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_items2,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()== R.id.logout)
{
FirebaseAuth.getInstance().signOut();
finish();
Intent intent=new Intent(this,LoginActivity.class);
startActivity(intent);
}
else if(item.getItemId()== R.id.feedback)
{
finish();
Intent intent=new Intent(this,FeedbackActivity.class);
startActivity(intent);
}
else if(item.getItemId()== R.id.publicPost)
{
finish();
Intent intent=new Intent(this,NewsFeedActivity.class);
startActivity(intent);
}
else if(item.getItemId()== R.id.timeline)
{
finish();
Intent intent=new Intent(this,TimelineActivity.class);
startActivity(intent);
}
else if(item.getItemId()== R.id.liveClass)
{
finish();
Intent intent=new Intent(this,LiveClassActivity.class);
startActivity(intent);
}
else if(item.getItemId()== R.id.home)
{
finish();
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);
}
return true;
}
}
ListView布局设计:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_margin="3dp"
android:background="#ffffff"
android:orientation="vertical"
android:padding="2dp"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:padding="5dp"
android:id="@+id/questionTV"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1. Which is the Capital city of Bangladesh?"
android:textColor="#180056"
/>
<LinearLayout
android:background="#EEE4E4"
android:padding="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioGroup
android:paddingLeft="12dp"
android:id="@+id/RG"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/RB1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Rangpur"
android:textColorHint="#000000"
android:textSize="15sp"
/>
<RadioButton
android:id="@+id/RB2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Dhaka"
android:textColorHint="#000000"
android:textSize="15sp"
/>
<RadioButton
android:id="@+id/RB3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Rajshahi"
android:textColorHint="#000000"
android:textSize="15sp"
/>
<RadioButton
android:id="@+id/RB4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Gobindagonj"
android:textColorHint="#000000"
android:textSize="15sp"
/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
适配器:
package com.example.sajib.smarticteducator;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import static com.example.sajib.smarticteducator.Activity.Quiz_Exam_Activity.questionAns;
public class Custom_Adapter_For_Quiz_Exam extends BaseAdapter {
String[] questions;
String[] A;
String[] B;
String[] C;
String[] D;
Context context;
LayoutInflater inflater;
public Custom_Adapter_For_Quiz_Exam(Context context, String[] questions, String[] A, String[] B, String[] C, String[] D)
{
this.context=context;
this.questions=questions;
this.A=A;
this.B=B;
this.C=C;
this.D=D;
}
@Override
public int getCount() {
return A.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.quiz_exam_layout_design,null);
}
TextView questionTV=convertView.findViewById(R.id.questionTV);
RadioGroup RG=convertView.findViewById(R.id.RG);
RadioButton RB1=convertView.findViewById(R.id.RB1);
RadioButton RB2=convertView.findViewById(R.id.RB2);
RadioButton RB3=convertView.findViewById(R.id.RB3);
RadioButton RB4=convertView.findViewById(R.id.RB4);
String serial=String.valueOf(position+1);
questionTV.setText(serial+". "+questions[position]);
RB1.setText(A[position]);
RB2.setText(B[position]);
RB3.setText(C[position]);
RB4.setText(D[position]);
//////////////////////////
RG.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
int childCount = group.getChildCount();
// ViewParent parent = group.;
for (int x = 0; x < childCount; x++) {
RadioButton btn = (RadioButton) group.getChildAt(x);
if (btn.getId() == checkedId) {
// int getPosition = (Integer) group.getTag();//do this
questionAns[position]=btn.getText().toString();
// System.out.println(btn.getText().toString());
// Toast.makeText(context,""+btn.getText().toString()+" Position: "+position,Toast.LENGTH_SHORT).show();
}
}
}
});
/////////////////////////
return convertView;
}
@Override
public int getViewTypeCount() {
//Count=Size of ArrayList.
return A.length;
}
@Override
public int getItemViewType(int position) {
return position;
}
}