Click here to see the display that is related to the xml file i posted
在模拟器中运行我的应用程序时没有出现任何错误,当我打开TestActivity时没有错误消息...问题是我看不到任何文本显示在它应该是的位置。
以下是活动的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:id="@+id/relatively"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fdb913"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="16dp"
tools:context="com.slbw.stoplookandbewarned.Testactivity"
android:weightSum="1"
android:gravity="center_vertical|center_horizontal|center">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.90"
android:orientation="vertical"
android:weightSum="1" >
<LinearLayout
android:id="@+id/linearLayout11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:orientation="vertical"
android:weightSum="1"
android:padding="0dp">
<TextView
android:id="@+id/tv_score"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:gravity="center"
android:text="SCORE"
android:textColor="#ffffff"
android:textSize="25.0sp"
android:textStyle="bold"
android:layout_weight="1"
android:textAlignment="textStart" />
<TextView
android:layout_width="67dp"
android:gravity="center"
android:textSize="18sp"
android:id="@+id/score"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.60"
android:orientation="vertical"
android:weightSum="1" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_weight="0.24"
android:layout_height="200dp"
android:gravity="center_vertical|center_horizontal">
<TextView
android:id="@+id/txtQuestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="35.0sp"
android:textStyle="bold"
android:visibility="invisible"
android:layout_alignBottom="@+id/imgQuestion"
android:layout_centerHorizontal="true"
android:layout_marginBottom="30dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imgQuestion"
android:layout_centerHorizontal="true"
android:visibility="invisible" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:orientation="vertical"
android:weightSum="1"
android:padding="10dp">
<Button
android:id="@+id/button1"
android:layout_width="267dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="80dp"
android:layout_marginRight="80dp"
android:layout_marginTop="5dp"
android:background="#fff"
android:gravity="center"
android:fontFamily="sans-serif-medium" />
<Button
android:id="@+id/button2"
android:layout_width="268dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="80dp"
android:layout_marginRight="80dp"
android:layout_marginTop="5dp"
android:background="#fff"
android:gravity="center"
android:textColor="#000000"
android:textSize="22.0sp" />
<Button
android:id="@+id/button3"
android:layout_width="267dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="80dp"
android:layout_marginRight="80dp"
android:layout_marginTop="5dp"
android:background="#fff"
android:gravity="center"
android:textColor="#000000"
android:textSize="22.0sp" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_counter"
android:layout_weight="0.20" />
</LinearLayout>
</LinearLayout>
以下是相关的类文件:
public class Testactivity extends AppCompatActivity {
List<Question> quesList;
int score;
int qid = 0;
//static int n;
Question currentQ;
QuizHelper db;
TextView scored, tv_score;
public TextView txtQuestion, txtCounter;
public Button button1, button2, button3;
public static String ans;
public static ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testactivity2);
QuizHelper db = new QuizHelper(this);
quesList = db.getAllQuestions();
currentQ = quesList.get(qid);
this.score = 0;
tv_score = (TextView) findViewById(R.id.score);
tv_score.setText(String.valueOf(score));
txtCounter = (TextView) findViewById(R.id.tv_counter);
db = new QuizHelper(this);
quesList = db.getAllQuestions();
currentQ = quesList.get(qid);
txtQuestion = (TextView) findViewById(R.id.txtQuestion);
img = (ImageView) findViewById(R.id.imgQuestion);
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
scored = (TextView) findViewById(R.id.score);
//db.getQuestion();
setQuestionView();
// button click listeners
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// passing the button text to other method
// to check whether the answer is correct or not
// same for all three buttons
getAnswer(button1.getText().toString());
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAnswer(button2.getText().toString());
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getAnswer(button3.getText().toString());
}
});
}
private void setQuestionView(){
//display control
txtQuestion.setText(currentQ.getQUESTION());
button1.setText(currentQ.getOPTA());
button2.setText(currentQ.getOPTB());
button3.setText(currentQ.getOPTC());
qid++;
}
public void getAnswer(String AnswerString) {
if (currentQ.getANSWER().equals(AnswerString)) {
// if conditions matches increase the int (score) by 1
// and set the text of the score view
score++;
scored.setText("" + this.score);
Toast.makeText(getApplicationContext(), "CORRECT!", Toast.LENGTH_SHORT).show();
//db.getQuestion();
} else {
Intent intent = new Intent(Testactivity.this, result.class);
// if unlucky start activity and finish the game
// Intent intent = new Intent(Testactivity.this, result.class);
Toast.makeText(getApplicationContext(), "WRONG, SORRY!", Toast.LENGTH_SHORT).show();
//db.getQuestion();
// db.getScore(test_entry.username, this.score);
// passing the int value
Bundle b = new Bundle();
b.putInt("score", score); // Your score
intent.putExtras(b); // Put your score to your next
startActivity(intent);
finish();
}
if(qid < 20 ) {
currentQ = quesList.get(qid);
setQuestionView();
}else{
Intent intent = new Intent(Testactivity.this,result.class);
Bundle b = new Bundle();
b.putInt("score", score);
intent.putExtras(b);
startActivity(intent);
finish();
}
}
}
这是问题类:
public class Question extends Activity{
private int ID;
private String Question, OPTA, OPTB, OPTC, ANSWER;
private int ISIMAGE;
public String getOPTA;
public String getOPTB;
public String getOPTC;
public int getIMG;
//public byte[] getIMG;
//public static int ID;
/* public static String QUESTION;
public static String OPTA;
public static String OPTB;
public static String OPTC;
public static int ISIMAGE;
public static String ANSWER;*/
public Question() {
ID = 0;
Question= "";
OPTA = "";
OPTB = "";
OPTC = "";
ANSWER = "";
ISIMAGE = Integer.parseInt("0");
}
public Question(String Question, String OPTA, String OPTB, String OPTC, String ANSWER, int ISIMAGE){
this.Question = Question;
this.OPTA = OPTA;
this.OPTB = OPTB;
this.OPTC = OPTC;
this.ISIMAGE = ISIMAGE;
this.ANSWER = ANSWER;
}
public int getID(){
return this.ID;
}
public String getQUESTION(){
return Question;
}
public String getOPTA(){
return OPTA;
}
public String getOPTB(){
return OPTB;
}
public String getOPTC(){
return OPTC;
}
public String getANSWER(){
return ANSWER;
}
public int getIMG(){
return ISIMAGE;
}
public void setID(int id) {
// this.ID = id;
}
public void setQUESTION(String qUESTION) {
this.Question = qUESTION;
}
public void setOPTA(String optA) {
this.OPTA = optA;
}
public void setOPTB(String optB) {
this.OPTB = optB;
}
public void setOPTC(String optC) {
this.OPTC = optC;
}
public void setANSWER(String ans) {
this.ANSWER = ans;
}
public void setISIMAGE(int ISIMAGE) {
this.ISIMAGE = ISIMAGE;
}
}
我一直在使用&#39;静态&#39;用于显示功能和变量。似乎没有工作。
这是我将它命名为QuizHelper.java的DBHelper
public class QuizHelper extends SQLiteOpenHelper {
Testactivity ta;
scores s;
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "mathsone";
// tasks table name
private static final String TABLE_NAME = "quest";
private static final String TABLE_USER = "userscore";
// tasks Table Columns names
private static final String KEY_ID = "qid";
private static final String KEY_QUES = "question";
private static final String KEY_ANSWER = "answer"; // correct option
private static final String KEY_OPTA = "opta"; // option a
private static final String KEY_OPTB = "optb"; // option b
private static final String KEY_OPTC = "optc"; // option c
private static final String KEY_ISIMAGE = "isimage"; // check if the question is an image
private static final String USER_NAME = "username";
private static final String USER_SCORE = "score";
private int ctr;
private SQLiteDatabase dba;
public QuizHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// onCreate(db);
}
@Override
public void onCreate(SQLiteDatabase db) {
dba = db;
ta = new Testactivity();
String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ( "
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES + " TEXT, "
+ KEY_OPTA + " TEXT, " + KEY_OPTB+ " TEXT, " + KEY_OPTC+ " TEXT, " + KEY_ANSWER
+ " TEXT, " + KEY_ISIMAGE + " INTEGER(11));";
db.execSQL(CREATE_TABLE);
addQuestion(db);
}
private void addQuestion(SQLiteDatabase db) {
Question q1 = new Question("A driver who got caught on drugs or alchohol is punishable by", "Suspension of license","Six months of imprisonment","All of the above","All of the above", 0);
this.addQuestiontoDB(q1,db);
Question q2 = new Question("What is the fine for driving without a valid license?", "Php 500.00","Php 500.00 and vehicle impounded","Php 3, 000.00","Php 3, 000.00", 0);
this.addQuestiontoDB(q2,db);
Question q3 = new Question("If you are caught, how many days does it take to fix your case and redeem the license", "30 days","15 days","10 days","15 days", 0);
this.addQuestiontoDB(q3,db);
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldV, int newV) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
// Adding new question
public void addQuestiontoDB(Question question, SQLiteDatabase db) {
// db = this.getWritableDatabase();
ContentValues values = new ContentValues();
//values.put(KEY_ID, Question.ID);
values.put(KEY_QUES, question.getQUESTION());
values.put(KEY_ANSWER, question.getANSWER());
values.put(KEY_OPTA, question.getOPTA);
values.put(KEY_OPTB, question.getOPTB);
values.put(KEY_OPTC, question.getOPTC);
values.put(KEY_ISIMAGE, question.getIMG);
// Inserting Row
db.insert(TABLE_NAME, null, values);
}
/*
public void getQuestion() {
Random r = new Random();
int n = r.nextInt(20);
db = this.getReadableDatabase();
ctr++;
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_QUEST + " WHERE `qid` = " + n + ";", null);
if (cursor.moveToFirst()) {
if(cursor.getInt(6) == 1) {
ta.txtQuestion.setVisibility(View.VISIBLE);
ta.txtQuestion.setText(cursor.getString(1).toString());
/*Log.e("IMAGE", "I WAS HERE");
ta.img.setVisibility(View.VISIBLE);
int imageResource = draw.getResources().getIdentifier("@drawable/"+cursor.getString(1), null, PACKAGE);
Drawable res = draw.getResources().getDrawable(imageResource);
ta.img.setImageDrawable(res);
//ta.img.setImageResource(imageResource);
} else {
ta.txtQuestion.setVisibility(View.VISIBLE);
ta.txtQuestion.setText(cursor.getString(1).toString());
}
ta.button1.setText(cursor.getString(3).toString());
ta.button2.setText(cursor.getString(4).toString());
ta.button3.setText(cursor.getString(5).toString());
ta.ans = cursor.getString(2).toString();
Log.e("Current Question ID", Integer.toString(n));
Testactivity.txtCounter.setText(Integer.toString(ctr));
}
}*/
public void getScore(String name, int score) {
dba = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(USER_NAME, name);
values.put(USER_SCORE, score);
dba.insert(TABLE_USER, null, values);
dba = this.getReadableDatabase();
Cursor cursor = dba.rawQuery("SELECT * FROM " + TABLE_USER + " ORDER BY " + USER_SCORE + " DESC, " + USER_SCORE + " ASC;", null);
s = new scores();
int ctr = cursor.getCount(), i;
Log.e("COUNT", Integer.toString(ctr));
if (ctr > 0) {
cursor.moveToFirst();
for(i = 0; i < ctr; i++) {
s.name += cursor.getString(0).toString() + "\n";
s.score += cursor.getString(1).toString() + "\n";
cursor.moveToNext();
}
}
}
public boolean removeScores() {
dba = this.getWritableDatabase();
dba.execSQL("DROP TABLE IF EXISTS " + TABLE_USER + ";");
return true;
}
public List<Question> getAllQuestions() {
List<Question> quesList = new ArrayList<Question>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_NAME;
dba = this.getReadableDatabase();
Cursor cursor = dba.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
//Question quest = new Question();
//quest.setID(cursor.getInt(0));
Question quest = new Question();
quest.setID(cursor.getInt(0));
quest.setQUESTION(cursor.getString(1));
quest.setANSWER(cursor.getString(5));
quest.setOPTA(cursor.getString(2));
quest.setOPTB(cursor.getString(3));
quest.setOPTC(cursor.getString(4));
quest.setISIMAGE(cursor.getInt(6));
quesList.add(quest);
} while (cursor.moveToNext());
}
// return quest list
cursor.close();
dba.close();
return quesList;
}
public int rowcount(){
int row = 0;
String selectQuery = "SELECT * FROM" + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, nul);
row = cursor.getCount();
return row;
}
}