首页 > 为什么Cursor放到带参数带返回值方法里就报错?

为什么Cursor放到带参数带返回值方法里就报错?

完整代码如下:
其中有大部分的代码被注释,那是可以成功运行的。
后来我想啊,游戏有好多种,但是需要统计的内容是一样的。
我就想传参 然后获得返回值的方式 来减少代码量啊。
但是把相关的查询代码放到 带参带返回值的方法中后 就报错了。
期间我debug了一下,发现 db = null 说明db的值没有哦。
所以我估计是db出了问题。
求各位大大看看有啥解决办法吧,谢谢!

package com.example.mscodescanner;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by jungege on 16/4/21.
 */
public class TicketsRecord extends AppCompatActivity{
    //注册控件
    private TextView mTv_TotalNum_Online; //中福在线 总张数
    private TextView mTv_SurplusNum_Online; //中福在线 剩余张数
    private TextView mTv_TotalValue_Online; //中福在线 总钱数
    private TextView mTv_SurplusValue_Online; //中福在线 剩余钱数
    //注册数据库
    private DatabaseContext dbContext;
    private SdCardDBHelper dbHelper;
    private SQLiteDatabase db;



    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.record_layout);

        init(); //初始化
        statistical(); //执行统计

    }

    private void init() {
        //实例化控件
        mTv_TotalNum_Online = (TextView) findViewById(R.id.tv_TotalNum_Online);
        mTv_SurplusNum_Online = (TextView) findViewById(R.id.tv_SurplusNum_Online);
        mTv_TotalValue_Online = (TextView) findViewById(R.id.tv_TotalValue_Online);
        mTv_SurplusValue_Online = (TextView) findViewById(R.id.tv_SurplusValue_Online);

        //实例化数据库
        dbContext = new DatabaseContext(TicketsRecord.this);
        dbHelper = new SdCardDBHelper(dbContext);
        db = dbHelper.getWritableDatabase(); //创建DB对象

    }

    private void statistical() {
        //中福在线
//        Cursor cursor_TotalNum = db.rawQuery("select * from Tickets where name = 0", null); //总张数
//        Cursor cursor_SurplusNum = db.rawQuery("select * from Tickets where name = 0 and use = 1", null); //剩余张数
//        Cursor cursor_TotalValue = db.rawQuery("select sum(price) from Tickets where name = 0", null); //总钱数
//        Cursor cursor_SurplusValue = db.rawQuery("select sum(price) from Tickets where name = 0 and use = 1", null); //剩余钱数
//
//        if (cursor_TotalNum.moveToFirst() && cursor_SurplusNum.moveToFirst() && cursor_TotalValue.moveToFirst() && cursor_SurplusValue.moveToFirst()){
//
//            int count_TotalNum = cursor_TotalNum.getCount();
//            int count_SurplusNum = cursor_SurplusNum.getCount();
//            int count_TotalValue = cursor_TotalValue.getInt(0);
//            int count_SurplusValue = cursor_SurplusValue.getInt(0);
//
//            mTv_TotalNum_Online.setText(String.valueOf(count_TotalNum));
//            mTv_SurplusNum_Online.setText(String.valueOf(count_TotalNum - count_SurplusNum));
//            mTv_TotalValue_Online.setText(String.valueOf(count_TotalValue));
//            mTv_SurplusValue_Online.setText(String.valueOf(count_TotalValue - count_SurplusValue));
//
//        } else {
//            mTv_TotalNum_Online.setText("没有记录");
//        }
//        //关闭游标
//        cursor_TotalNum.close();
//        cursor_SurplusNum.close();
//        cursor_TotalValue.close();
//        cursor_SurplusValue.close();

        TicketsRecord hello = new TicketsRecord();
        int wellcome = hello.statManage(0);
        mTv_TotalNum_Online.setText(String.valueOf(wellcome));
    }

    //Statistical management
    private int statManage(int name){


        //Cursor cursor_TotalNum = db.rawQuery("select sum(price) from Tickets where name = ?", new String[]{String.valueOf(name)});
        Cursor cursor_TotalNum = db.rawQuery("select sum(price) from Tickets where name = " + name, null); //总张数

        cursor_TotalNum.moveToFirst();
        int count_TotalNum = cursor_TotalNum.getColumnIndex("sum(price)");

        cursor_TotalNum.close();

        return count_TotalNum;

        //return "欢迎您" + name + "!";
    }
}

你确定你这样的写法可以吗?你试一下这样写?:
Cursor cursor_TotalNum = db.rawQuery("select sum(price) from Tickets where name = ?", new String[]{name});


具体是什么错误,贴出来看看啊


你不需要new TicketsRecord();直接调用statManage(0);即可


根据 idisfkj 提供的答案,程序成功运行,非常感谢。同时在这里贴上修改后的代码。供遇到同样问题的同学参考。

package com.example.mscodescanner;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Arrays;

/**
 * Created by jungege on 16/4/21.
 */
public class TicketsRecord extends AppCompatActivity{
    //注册控件
    private TextView mTv_TotalNum_Online; //中福在线 总张数
    private TextView mTv_SurplusNum_Online; //中福在线 剩余张数
    private TextView mTv_TotalValue_Online; //中福在线 总钱数
    private TextView mTv_SurplusValue_Online; //中福在线 剩余钱数
    //注册数据库
    private DatabaseContext dbContext;
    private SdCardDBHelper dbHelper;
    private SQLiteDatabase db;



    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.record_layout);

        init(); //初始化
        statistical(); //执行统计

    }

    private void init() {
        //实例化控件
        mTv_TotalNum_Online = (TextView) findViewById(R.id.tv_TotalNum_Online);
        mTv_SurplusNum_Online = (TextView) findViewById(R.id.tv_SurplusNum_Online);
        mTv_TotalValue_Online = (TextView) findViewById(R.id.tv_TotalValue_Online);
        mTv_SurplusValue_Online = (TextView) findViewById(R.id.tv_SurplusValue_Online);

        //实例化数据库
        dbContext = new DatabaseContext(TicketsRecord.this);
        dbHelper = new SdCardDBHelper(dbContext);
        db = dbHelper.getWritableDatabase(); //创建DB对象

    }

    private void statistical() {

        //中福在线
        int[] online_result = statManage(0);
        mTv_TotalNum_Online.setText(String.valueOf(online_result[0]));
        mTv_SurplusNum_Online.setText(String.valueOf(online_result[1]));
        mTv_TotalValue_Online.setText(String.valueOf(online_result[2]));
        mTv_SurplusValue_Online.setText(String.valueOf(online_result[3]));

    }

    //Statistical management
    private int[] statManage(int name){
        int[] tempScores = new int[4];

        Cursor cursor_TotalNum = db.rawQuery("select * from Tickets where name = " + name, null); //总张数
        Cursor cursor_SurplusNum = db.rawQuery("select * from Tickets where name = ? and use = 1", new String[]{String.valueOf(name)}); //剩余张数
        Cursor cursor_TotalValue = db.rawQuery("select sum(price) from Tickets where name = " + name, null); //总钱数
        Cursor cursor_SurplusValue = db.rawQuery("select sum(price) from Tickets where name = ? and use = 1", new String[]{String.valueOf(name)}); //剩余钱数

        cursor_TotalNum.moveToNext();
        cursor_SurplusNum.moveToNext();
        cursor_TotalValue.moveToNext();
        cursor_SurplusValue.moveToNext();

        tempScores[0] = cursor_TotalNum.getCount(); //总张数
        tempScores[1] = cursor_SurplusNum.getCount(); //已用张数
        tempScores[1] = tempScores[0] - tempScores[1]; //总张数 - 已用 = 剩余张数
        tempScores[2] = cursor_TotalValue.getInt(cursor_TotalValue.getColumnIndex("sum(price)")); //总钱数
        tempScores[3] = cursor_SurplusValue.getInt(cursor_SurplusValue.getColumnIndex("sum(price)")); //已用钱数
        tempScores[3] = tempScores[2] - tempScores[3]; //总钱数 - 已用 = 剩余钱数

        cursor_TotalNum.close();
        cursor_SurplusNum.close();
        cursor_TotalValue.close();
        cursor_SurplusValue.close();

        return tempScores;
    }

}
【热门文章】
【热门文章】