2 sinat 22315343 sinat_22315343 于 2014.12.20 16:34 提问

脚标越界求助,调了好久,还请路过大神顺带帮解决。。。

代码如下
package com.example.datebase;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DBHelper {
private static final String DATABASE_NAME = "";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "student";
private static final String[] COLUMNS = { "id", "name", "score" };
private String sql = "";
private DBOpenHelper helper;
private SQLiteDatabase db;

public DBHelper(Context context) {

    sql = "create table" + TABLE_NAME + "(" + COLUMNS[0]
            + " integer primary key," + COLUMNS[1] + " varchar(50),"
            + COLUMNS[2] + " integer);";
    helper = new DBOpenHelper(context, DATABASE_NAME, DATABASE_VERSION,
            TABLE_NAME, sql);

    db = helper.getWritableDatabase();

}

public void insert(Student data) {
    ContentValues values = new ContentValues();
    values.put(COLUMNS[0], data.getId());
    values.put(COLUMNS[1], data.getName());
    values.put(COLUMNS[2], data.getScore());
    db.insert(TABLE_NAME, null, values);

}

public ArrayList<Student> find() {
    ArrayList<Student> list = new ArrayList<Student>();
    Student stu = null;

    Cursor cursor = db.query(TABLE_NAME, COLUMNS, null, null, null, null,
            null);

    while (cursor.moveToNext()) {
        stu = new Student();
        stu.setId(cursor.getInt(0));
        stu.setName(cursor.getString(1));
        stu.setScore(cursor.getInt(2));
        list.add(stu);
    }
    cursor.close();

    return list;

}

}

package com.example.datebase;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBOpenHelper extends SQLiteOpenHelper {

private String CREATE_TABLE = "";
private String tableName = "";

public DBOpenHelper(Context context, String dbName, int dbVersion,
        String tableName, String sql) {
    super(context, dbName, null, dbVersion);
    // TODO Auto-generated constructor stub
    CREATE_TABLE = sql;

    this.tableName = tableName;

}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub
    db.execSQL(CREATE_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub
    db.execSQL("drop table if exists" + tableName);
    onCreate(db);

}

}

package com.example.datebase;

import java.util.ArrayList;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
private EditText id, name, score, list;
private Button save, read;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    id = (EditText) findViewById(R.id.editText1);
    name = (EditText) findViewById(R.id.editText2);
    score = (EditText) findViewById(R.id.editText3);
    save = (Button) findViewById(R.id.button1);
    read = (Button) findViewById(R.id.button2);
    list = (EditText) findViewById(R.id.editText4);
    save.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Student info = null;
            DBHelper helper = new DBHelper(MainActivity.this);// 这句有问题
            info = new Student();
            info.setId(Integer.parseInt(id.getText().toString()));
            info.setName(name.getText().toString());
            info.setScore(Integer.parseInt(score.getText().toString()));
            helper.insert(info);
            id.setText("");
            name.setText("");
            score.setText("");
            Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT)
                    .show();

        }
    });
    read.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            list.setText("");
            DBHelper helper = new DBHelper(MainActivity.this);
            ArrayList<Student> Stulist = helper.find();
            for (Student info : Stulist) {
                list.setText(list.getText().toString() + info.getId() + ""
                        + info.getName() + "" + info.getScore() + "\n");
            }

        }
    });

4个回答

sinat_22315343
sinat_22315343   2014.12.20 16:38

一般没人回答吗?

bdmh
bdmh   Ds   Rxr 2014.12.20 16:43

我去,你想干嘛,知道是越界,自己打个断点,不会调试吗

sinat_22315343
sinat_22315343 打过了 运行到源码就不行了
接近 3 年之前 回复
u013337963
u013337963   2014.12.20 16:52

自己先打断电一步一步的调试吧

sinat_22315343
sinat_22315343 打过了 运行到源码就不行了
接近 3 年之前 回复
sinat_22315343
sinat_22315343 打过了 运行到源码就不行了
接近 3 年之前 回复
xingjunfeng1982
xingjunfeng1982   2014.12.20 20:56

degug进入到DBHelper类里,看是sql、helper 、db中哪一行报的错,观察下String[] COLUMNS的状态

话说回来,你好歹也贴个报错的日志吧,你这样光贴个代码让人怎么帮你啊

Csdn user default icon
上传中...
上传图片
插入图片