1

我正在尝试构建一个将一些信息存储在 SQLite 表中的小型应用程序。但我总是在 getWritableDatabase 方法中收到“Java.lang.NullPointerException”异常。谁能帮我?所有代码都在下面。

显现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dbtest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.dbtest.Principal"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

活动(activity_principal.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Principal" 
    android:orientation="horizontal">
</LinearLayout>

字符串.XML:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">DBTest</string>
    <string name="menu_settings">Config</string>
</resources>

爪哇:

package com.example.dbtest;

public class Prova {

    String _codigo;
    String _nome;
    String _campeonato_codigo;
    String _campeonato_nome;

    public Prova(){
    }

    public Prova(String _codigo, String _nome, String _campeonato_codigo, String _campeonato_nome) {
        this._codigo = _codigo;
        this._nome = _nome;
        this._campeonato_codigo = _campeonato_codigo;
        this._campeonato_nome = _campeonato_nome;
        }

    public String get_codigo(){ return this._codigo; }
    public void set_codigo(String _codigo){ this._codigo = _codigo; }

    public String get_nome(){ return this._nome; }
    public void set_nome(String _nome){ this._nome = _nome; }

    public String get_campeonato_codigo(){ return this._campeonato_codigo; }
    public void set_campeonato_codigo(String _campeonato_codigo){ this._campeonato_codigo = _campeonato_codigo; }

    public String get_campeonato_nome(){ return this._campeonato_nome; }
    public void set_campeonato_nome(String _campeonato_nome){ this._campeonato_nome = _campeonato_nome; }

}


package com.example.dbtest;

import android.app.Activity;
import android.os.Bundle;

public class Principal extends Activity {

    DownloadProva DP = new DownloadProva();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        DP.baixarProva();

        setContentView(R.layout.activity_principal);

    };

}


package com.example.dbtest;

import android.app.Activity;

public class DownloadProva extends Activity {

    public String strNomeProva = "";

    public void baixarProva() {

        String strCampeonatoCodigo = "1";
        String strCampeonatoNome = "dois";
        String strEtapaCodigo = "3";
        String strEtapaNome = "quatro";

        DatabaseHelper mydb = new DatabaseHelper(getBaseContext());

        mydb.addProva(new Prova( strCampeonatoCodigo, strCampeonatoNome, strEtapaCodigo, strEtapaNome));

    };

};




package com.example.dbtest;

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

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "dbtest.db";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public static final String TABLE_NAME_PROVA = "prova";
    public static final String COLUMN_NAME_PROVA_CODIGO                 = "codigo";
    public static final String COLUMN_NAME_PROVA_NOME                   = "nome";
    public static final String COLUMN_NAME_PROVA_CAMPEONATO_CODIGO      = "campeonato_codigo";
    public static final String COLUMN_NAME_PROVA_CAMPEONATO_NOME        = "campeonato_nome";

    public static final String TEXT_TYPE = " TEXT";
    public static final String COMMA_SEP = ",";
    public static final String PRIMARY_KEY = " PRIMARY KEY ";

    @Override
    public void onCreate(SQLiteDatabase db) {

        final String SQL_CREATE_PROVA = 
            "CREATE TABLE " + TABLE_NAME_PROVA + " (" +
            COLUMN_NAME_PROVA_CODIGO             + TEXT_TYPE + COMMA_SEP +
            COLUMN_NAME_PROVA_NOME               + TEXT_TYPE + COMMA_SEP +
            COLUMN_NAME_PROVA_CAMPEONATO_CODIGO  + TEXT_TYPE + COMMA_SEP +
            COLUMN_NAME_PROVA_CAMPEONATO_NOME    + TEXT_TYPE + " )";
        db.execSQL(SQL_CREATE_PROVA);

    };

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME_PROVA);

        onCreate(db);
    }

    public void addProva(Prova prova) {

        try {
            // ****** fires "java.lang.NullPointerException" **********
            SQLiteDatabase db = this.getWritableDatabase(); 

            ContentValues values = new ContentValues();
            values.put(COLUMN_NAME_PROVA_CODIGO, prova.get_codigo());
            values.put(COLUMN_NAME_PROVA_NOME, prova.get_nome());
            values.put(COLUMN_NAME_PROVA_CAMPEONATO_CODIGO, prova.get_campeonato_codigo());
            values.put(COLUMN_NAME_PROVA_CAMPEONATO_NOME, prova.get_campeonato_nome());

            db.delete(TABLE_NAME_PROVA, null, null);
            db.insert(TABLE_NAME_PROVA, null, values);
            db.close();
            return;
        }
        catch ( Exception e ) {
            return;
        }        

    }

    public Prova getProva(String codigo) {

        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_NAME_PROVA, new String[] { COLUMN_NAME_PROVA_CODIGO, 
            COLUMN_NAME_PROVA_NOME, COLUMN_NAME_PROVA_CAMPEONATO_CODIGO,
            COLUMN_NAME_PROVA_CAMPEONATO_NOME }, 
            null, null, null, null, null, null);

        if (cursor != null)
            cursor.moveToFirst();

        Prova prova = new Prova(cursor.getString(0), cursor.getString(1), cursor.getString(2), 
            cursor.getString(3));

        db.close();
        return prova;
    }

}
4

2 回答 2

1
  1. 您可以使用调试来准确查看希望行是否给您 Nullpointerexception。
  2. 它可能: db.delete(TABLE_NAME_PROVA, null, null); //删除这个
  3. 如果您是新手,本教程可以帮助您更多地了解 Sqlite:http ://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite /
于 2013-01-28T01:46:53.653 回答
0

自己解决(初学者的小错误:( ...):

public class Principal extends Activity {

    ...

        // wrong >>>>> DP.baixarProva();
        DP.baixarProva(getApplicationContext());

...

public class DownloadProva extends Activity {

    ...

    // wrong >>>>> public void baixarProva() {
    public void baixarProva(Context myContext) {

        ...

        // wrong >>>>> DatabaseHelper mydb = new DatabaseHelper(getBaseContext());
        DatabaseHelper mydb = new DatabaseHelper(myContext);
于 2013-01-29T18:29:50.793 回答