_詹某_ 2017-11-13 08:05 采纳率: 0%
浏览 1009

请问我这个东西错误在哪,看了半天看不出来,求大佬帮我看下

图片说明
![图片说明图片说明
图片说明图片说明图片说明

package com.example.answers;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import android.R.bool;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Checkable;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

/**

  • 答题系统 */

public class MainActivity extends Activity {

// 数据库名称
private String DB_NAME = "question.db";
// 数据库的地址
private String DB_PATH = "/data/data/com.example.answers/databases/";
// 总的题目数据
private int count;
// 当前显示的题目
private int corrent;
// 问题
private TextView tv_title;
// 选项
RadioButton[] mRadioButton = new RadioButton[4];
// 上一题
private Button btn_up;
// 下一题
private Button btn_down;
// 详情
private TextView tv_result;
// 容器
private RadioGroup mRadioGroup;
// 是否进入错题模式
private boolean wrongMode;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initFile();
    initView();
    initDB();

}

/**
 * 初始化View
 */
private void initView() {

    wrongMode = false;

    tv_title = (TextView) findViewById(R.id.tv_title);

    mRadioButton[0] = (RadioButton) findViewById(R.id.RadioA);
    mRadioButton[1] = (RadioButton) findViewById(R.id.RadioB);
    mRadioButton[2] = (RadioButton) findViewById(R.id.RadioC);
    mRadioButton[3] = (RadioButton) findViewById(R.id.RadioD);

    btn_down = (Button) findViewById(R.id.btn_down);
    btn_up = (Button) findViewById(R.id.btn_up);

    tv_result = (TextView) findViewById(R.id.tv_result);

    mRadioGroup = (RadioGroup) findViewById(R.id.mRadioGroup);

}

/**
 * 初始化数据库服务
 */

private void initDB() {
    DBService dbService = new DBService();
    final List<Question> list = dbService.getQuestion();

    count = list.size();
    corrent = 0;

    Question q = list.get(0);
    tv_title.setText(q.question);


    mRadioButton[0].setText(q.answerA);
    mRadioButton[1].setText(q.answerB);
    mRadioButton[2].setText(q.answerC);
    mRadioButton[3].setText(q.answerD);

    // 上一题
    btn_up.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if (corrent > 0) {
                corrent--;

                Question q = list.get(corrent);

                tv_title.setText(q.question);

                mRadioButton[0].setText(q.answerA);
                mRadioButton[1].setText(q.answerB);
                mRadioButton[2].setText(q.answerC);
                mRadioButton[3].setText(q.answerD);

                tv_result.setText(q.explaination);

                mRadioGroup.clearCheck();

                // 设置选中
                if (q.selectedAnswer != -1) {
                    mRadioButton[q.selectedAnswer].setChecked(true);
                }

            }

        }
    });

    // 下一题
    btn_down.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // 判断是否为最后一题
            if (corrent < count - 1) {
                corrent++;
                Question q = list.get(corrent);

                tv_title.setText(q.question);

                mRadioButton[0].setText(q.answerA);
                mRadioButton[1].setText(q.answerB);
                mRadioButton[2].setText(q.answerC);
                mRadioButton[3].setText(q.answerD);

                tv_result.setText(q.explaination);

                mRadioGroup.clearCheck();

                // 设置选中
                if (q.selectedAnswer != -1) {
                    mRadioButton[q.selectedAnswer].setChecked(true);
                }
            } else if (corrent == count - 1 && wrongMode == true) {

                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("提示")
                        .setMessage("已经到达最后一题,是否退出?")
                        .setPositiveButton("确定",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(
                                            DialogInterface dialog,
                                            int which) {
                                        finish();

                                    }
                                }).setNegativeButton("取消", null).show();

            } else {
                // 没有题目了,开始验证正确性
                final List<Integer> wrongList = checkAnswer(list);

                if (wrongList.size() == 0) {
                    new AlertDialog.Builder(MainActivity.this)
                            .setTitle("提示")
                            .setMessage("很棒哦")
                            .setPositiveButton("确定",
                                    new DialogInterface.OnClickListener() {

                                        @Override
                                        public void onClick(
                                                DialogInterface dialog,
                                                int which) {
                                            finish();

                                        }
                                    }).setNegativeButton("取消", null).show();
                }

                // 窗口提示
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("恭喜,答题完成!")
                        .setMessage(
                                "恭喜你本次答题"
                                        + (list.size() - wrongList.size())*2
                                        + "分" + "\n" + "答错了"
                                        + wrongList.size() + "道题" + "\n"
                                        + "是否查看错题?")
                        .setPositiveButton("确定",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(
                                            DialogInterface dialog,
                                            int which) {
                                        wrongMode = true;
                                        List<Question> newList = new ArrayList<Question>();
                                        for (int i = 0; i < wrongList
                                                .size(); i++) {
                                            newList.add(list.get(wrongList
                                                    .get(i)));
                                        }
                                        list.clear();
                                        for (int i = 0; i < newList.size(); i++) {
                                            list.add(newList.get(i));
                                        }
                                        corrent = 0;
                                        count = list.size();

                                        // 更新当前显示的内容
                                        Question q = list.get(corrent);

                                        tv_title.setText(q.question);

                                        mRadioButton[0].setText(q.answerA);
                                        mRadioButton[1].setText(q.answerB);
                                        mRadioButton[2].setText(q.answerC);
                                        mRadioButton[3].setText(q.answerD);

                                        tv_result.setText(q.explaination);
                                        // 显示结果
                                        tv_result
                                                .setVisibility(View.VISIBLE);
                                    }
                                })
                        .setNegativeButton("取消",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(
                                            DialogInterface dialog, int which) {
                                        finish();

                                    }
                                }).show();

            }

        }

    });
    // 答案选中
    mRadioGroup
            .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    for (int i = 0; i < 4; i++) {
                        if (mRadioButton[i].isChecked() == true) {
                            list.get(corrent).selectedAnswer = i;
                            break;
                        }
                    }

                }
            });

}

/*
 * 判断是否答题正确
 */
private List<Integer> checkAnswer(List<Question> list) {
    List<Integer> wrongList = new ArrayList<>();
    for (int i = 0; i < list.size(); i++) {
        // 判断对错
        if (list.get(i).answer != list.get(i).selectedAnswer) {
            wrongList.add(i);
        }
    }
    return wrongList;
}

/*
 * 将数据库拷贝到相应目录
 */

private void initFile() {
    // 判断数据库是否拷贝到相应的目录下
    if (new File(DB_PATH + DB_NAME).exists() == false) {
        File dir = new File(DB_PATH);
        if (!dir.exists()) {
            dir.mkdir();
        }

        // 复制文件
        try {
            InputStream is = getBaseContext().getAssets().open(DB_NAME);
            OutputStream os = new FileOutputStream(DB_PATH + DB_NAME);

            // 用来复制文件
            byte[] buffer = new byte[1024];
            // 保存已经复制的长度
            int length;

            // 开始复制
            while ((length = is.read(buffer)) > 0) {
                os.write(buffer, 0, length);

            }

            // 刷新
            os.flush();
            // 关闭
            os.close();
            is.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

}

  • 写回答

5条回答 默认 最新

  • 海之沐 2017-11-13 08:09
    关注

    空指针呀,按照行号看一下,开调试跟一下。你这代码太多太乱了,不好看

    评论

报告相同问题?

悬赏问题

  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题