Java 如何将另一个按钮添加到预先制作的android程序(android Studio)

Java 如何将另一个按钮添加到预先制作的android程序(android Studio),java,android,Java,Android,我正在使用这个,但我想让每个问题有4个选项。我还把问题减少到5个。有人能帮忙吗?这是修改后的代码。但它不会运行:( QuizActivity.java public class QuestionActivity extends Activity { List<Question> quesList; int score = 0; int qid = 0; Question currentQ; TextView txtQuestion, times

我正在使用这个,但我想让每个问题有4个选项。我还把问题减少到5个。有人能帮忙吗?这是修改后的代码。但它不会运行:(

QuizActivity.java

public class QuestionActivity extends Activity {
    List<Question> quesList;
    int score = 0;
    int qid = 0;
    Question currentQ;
    TextView txtQuestion, times, scored;
    Button button1, button2, button3, button4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        QuizHelper db = new QuizHelper(this);  
        quesList = db.getAllQuestions();
        currentQ = quesList.get(qid);
        txtQuestion = (TextView) findViewById(R.id.txtQuestion);
        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);
        scored = (TextView) findViewById(R.id.score);
        times = (TextView) findViewById(R.id.timers);
        setQuestionView();
        times.setText("00:02:00");
        CounterClass timer = new CounterClass(60000, 1000);
        timer.start();
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                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());
            }
        });
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getAnswer(button4.getText().toString());
            }
        });
    }
    public void getAnswer(String AnswerString) {
        if (currentQ.getANSWER().equals(AnswerString)) {
            score++;
            scored.setText("Score : " + score);
        } else {
            Intent intent = new Intent(QuestionActivity.this,
                    ResultActivity.class);
            Bundle b = new Bundle();
            b.putInt("score", score); // Your score
            intent.putExtras(b); // Put your score to your next
            startActivity(intent);
            finish();
        }
        if (qid < 4) {
            currentQ = quesList.get(qid);
            setQuestionView();
        } else {
            Intent intent = new Intent(QuestionActivity.this,
                    ResultActivity.class);
            Bundle b = new Bundle();
            b.putInt("score", score); // Your score
            intent.putExtras(b); // Put your score to your next
            startActivity(intent);
            finish();
        }
    }
    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @SuppressLint("NewApi")
    public class CounterClass extends CountDownTimer {
        public CounterClass(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }
        @Override
        public void onFinish() {
            times.setText("Time is up");
        }
        @Override
        public void onTick(long millisUntilFinished) {
            long millis = millisUntilFinished;
            String hms = String.format(
                    "%02d:%02d:%02d",
                    TimeUnit.MILLISECONDS.toHours(millis),
                    TimeUnit.MILLISECONDS.toMinutes(millis)
                            - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS
                            .toHours(millis)),
                    TimeUnit.MILLISECONDS.toSeconds(millis)
                            - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
                            .toMinutes(millis)));
            System.out.println(hms);
            times.setText(hms);
        }
    }
    private void setQuestionView() {
        txtQuestion.setText(currentQ.getQUESTION());
        button1.setText(currentQ.getOPTA());
        button2.setText(currentQ.getOPTB());
        button3.setText(currentQ.getOPTC());
        button4.setText(currentQ.getOPTD());
        qid++;
    }
}
然后,对于activity_main.xml,我添加了一个id为button4的android按钮

<Button
       android:id="@+id/button4"
       android:layout_width="fill_parent"
       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:text="31"
       android:textColor="#000000"
       android:textSize="25.0sp" />
在表创建sql中,
KEY\u OPTC
后面缺少一个逗号


在表创建sql中,
KEY\u OPTC
之后缺少了一个逗号。

您说它没有运行。您可能会遇到编译错误,或者在某个时候出现堆栈跟踪。请编辑您的问题以包含编译错误或堆栈跟踪。尝试只发布代码的重要部分。logcat会告诉您发生了什么。tl;drYou说它没有运行。您可能会在某个时候遇到编译错误或堆栈跟踪。请编辑您的问题以包含编译错误或堆栈跟踪。请尝试只发布代码的重要部分。logcat会告诉您发生了什么。tl;dr程序仍然没有运行:(我仍然收到相同的logcat错误尝试从手机中删除你的应用程序,然后再次运行。在你的SqliteOpenHelper中放置一些断点,以查看sql语句是否正确执行。在你的logcat中查找SQLite异常,它们可能不会使你的应用程序崩溃,但表明重新创建表有问题。如果应用程序在第6列失败,这意味着hat other 5正常,因此您的数据库尚未正确升级程序仍无法运行:(我仍然收到相同的logcat错误尝试从手机中删除你的应用程序,然后再次运行。在你的SqliteOpenHelper中放置一些断点,以查看sql语句是否正确执行。在你的logcat中查找SQLite异常,它们可能不会使你的应用程序崩溃,但表明重新创建表有问题。如果应用程序在第6列失败,这意味着其他5个还可以,所以您的数据库没有进行适当升级
public class Question extends Activity {
    private int ID;
    private String QUESTION;
    private String OPTA;
    private String OPTB;
    private String OPTC;
    private String OPTD;
    private String ANSWER;
    public Question() {
        ID = 0;
        QUESTION = "";
        OPTA = "";
        OPTB = "";
        OPTC = "";
        OPTD = "";
        ANSWER = "";
    }
    public Question(String qUESTION, String oPTA, String oPTB, String oPTC,String oPTD,
                    String aNSWER) {
        QUESTION = qUESTION;
        OPTA = oPTA;
        OPTB = oPTB;
        OPTC = oPTC;
        OPTD = oPTD;
        ANSWER = aNSWER;
    }
    public int getID() {
        return ID;
    }
    public String getQUESTION() {
        return QUESTION;
    }
    public String getOPTA() {
        return OPTA;
    }
    public String getOPTB() {
        return OPTB;
    }
    public String getOPTC() {
        return OPTC;
    }
    public String getOPTD() {
        return OPTD;
    }
    public String getANSWER() {
        return ANSWER;
    }
    public void setID(int id) {
        ID = id;
    }
    public void setQUESTION(String qUESTION) {
        QUESTION = qUESTION;
    }
    public void setOPTA(String oPTA) {
        OPTA = oPTA;
    }
    public void setOPTB(String oPTB) {
        OPTB = oPTB;
    }
    public void setOPTC(String oPTC) {
        OPTC = oPTC;
    }
    public void setOPTD(String oPTD) {
        OPTD = oPTD;
    }
    public void setANSWER(String aNSWER) {
        ANSWER = aNSWER;
    }
}
<Button
       android:id="@+id/button4"
       android:layout_width="fill_parent"
       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:text="31"
       android:textColor="#000000"
       android:textSize="25.0sp" />
FATAL EXCEPTION: main
Process: versionone.example, PID: 2252
java.lang.RuntimeException: Unable to start activity ComponentInfo{versionone.example/versionone.example.QuestionActivity}: java.lang.IllegalStateException: Couldn't read row 0, col 6 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
   at android.app.ActivityThread.access$800(ActivityThread.java:135)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:136)
   at android.app.ActivityThread.main(ActivityThread.java:5001)
   at java.lang.reflect.Method.invokeNative(Native Method)
   at java.lang.reflect.Method.invoke(Method.java:515)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
   at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 6 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
   at android.database.CursorWindow.nativeGetString(Native Method)
   at android.database.CursorWindow.getString(CursorWindow.java:434)
   at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
   at versionone.example.QuizHelper.getAllQuestions(QuizHelper.java:90)
   at versionone.example.QuestionActivity.onCreate(QuestionActivity.java:38)
   at android.app.Activity.performCreate(Activity.java:5231)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
   at android.app.ActivityThread.access$800(ActivityThread.java:135) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
   at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:136) 
   at android.app.ActivityThread.main(ActivityThread.java:5001) 
   at java.lang.reflect.Method.invokeNative(Native Method) 
   at java.lang.reflect.Method.invoke(Method.java:515) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
   at dalvik.system.NativeStart.main(Native Method) 
    + KEY_OPTB + " TEXT, " + KEY_OPTC + " TEXT, " + KEY_OPTD + " TEXT)";