-2

美好的一天。我有以下小应用程序。每当它到达螺栓线时,应用程序就会停止。在那之后我已经注释掉了这些行,只是为了测试错误发生的位置。我还附上了 DataHelper 以供您参考。我是 eclipse 的新手,请您看看我做错了什么。

package com.example.*****;

import android.os.Bundle;
import android.view.Menu;
import android.app.Activity;
import java.util.List;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView output;
    private DataHelper dh;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    this.output=(TextView) this.findViewById(R.id.out_text);
        **this.dh = new DataHelper(this);**
    //  this.dh.deleteAll();
    //  this.dh.insert("Porkey pig");
    //  this.dh.insert("Foghorn");
    //  List<String> names = this.dh.selectAll();
    //  StringBuilder sb = new StringBuilder();
    //  sb.append("Names in database:\n");

//          for (String name : names) {
    //      sb.append(name + "\n");
    //  }

    //  Log.d("EXAMPLE","names size - " + names.size());
    //  this.output.setText(sb.toString());
    }

//  @Override
//  public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
    //  getMenuInflater().inflate(R.menu.main, menu);
    //  return true;
//  }

}

数据助手

package com.example.*****;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;

public class DataHelper {
    private static final String DATABASE_NAME = "whkgyn.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME = "attend";

    private Context context;
    private SQLiteDatabase db;

    private SQLiteStatement insertStmt;
    private static final String INSERT = "insert into "
            + TABLE_NAME + "(name) values (?)";

    public DataHelper(Context context){
        this.context = context;
        OpenHelper openhelper = new OpenHelper(this.context);
        this.db = this.db;

        //this.db = OpenHelper.getWritableDatabase();
        this.insertStmt = this.db.compileStatement(INSERT);
    }

    public long insert(String name){
        this.insertStmt.bindString(1,name);
        return this.insertStmt.executeInsert();
    }

    public void deleteAll() {
        this.db.delete(TABLE_NAME, null,null);
    }

    public List <String> selectAll(){
        List<String> list = new ArrayList<String>();
        Cursor cursor = this.db.query(TABLE_NAME, new String[] {"name"},
                null, null, null, null,"name desc");

        if (cursor.moveToFirst()){
            do {
                list.add(cursor.getString(0));
            } while (cursor.moveToNext());
        }

        if (cursor != null && !cursor.isClosed()){
            cursor.close();
            }

        return list;
        }

    private static class OpenHelper extends SQLiteOpenHelper {
        OpenHelper(Context context) {
            super(context, DATABASE_NAME,null,DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db){
                db.execSQL("CREATE TABLE " + TABLE_NAME + "(id INTEGER PRIMARY KEY, name  TEXT)");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
            Log.w("Example","Upgrading database, this will drop tables and recrate");
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
        }
    }



    }
4

1 回答 1

0

我可以知道错误是什么,但是您可以尝试:

this.dh = new DataHelper(getApplicationContext());
于 2013-10-04T10:09:10.777 回答