基本SQLite应用程序抛出运行时异常

时间:2014-03-18 14:49:56

标签: java android eclipse sqlite

我是android编程新手,我正在尝试开发一个基本的SQLite应用程序,它抛出运行时异常。为此,我将代码分为三个类,即Mainactivity,DBhelper和Message。我没有得到代码中的问题。我在下面给出了在AVD中运行的Logcat。

03-18 20:07:56.904: E/Trace(736): error opening trace file: No such file or directory (2)
03-18 20:07:57.434: D/AndroidRuntime(736): Shutting down VM
03-18 20:07:57.474: W/dalvikvm(736): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
03-18 20:07:57.494: E/AndroidRuntime(736): FATAL EXCEPTION: main
03-18 20:07:57.494: E/AndroidRuntime(736): java.lang.RuntimeException: Unable to start activity ComponentInfo{one.example.sqlitetesting/one.example.sqlitetesting.MainActivity}: java.lang.NullPointerException
03-18 20:07:57.494: E/AndroidRuntime(736):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-18 20:07:57.494: E/AndroidRuntime(736):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-18 20:07:57.494: E/AndroidRuntime(736):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-18 20:07:57.494: E/AndroidRuntime(736):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-18 20:07:57.494: E/AndroidRuntime(736):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 20:07:57.494: E/AndroidRuntime(736):  at android.os.Looper.loop(Looper.java:137)
03-18 20:07:57.494: E/AndroidRuntime(736):  at android.app.ActivityThread.main(ActivityThread.java:4745)
03-18 20:07:57.494: E/AndroidRuntime(736):  at java.lang.reflect.Method.invokeNative(Native Method)
03-18 20:07:57.494: E/AndroidRuntime(736):  at java.lang.reflect.Method.invoke(Method.java:511)
03-18 20:07:57.494: E/AndroidRuntime(736):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-18 20:07:57.494: E/AndroidRuntime(736):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-18 20:07:57.494: E/AndroidRuntime(736):  at dalvik.system.NativeStart.main(Native Method)
03-18 20:07:57.494: E/AndroidRuntime(736): Caused by: java.lang.NullPointerException
03-18 20:07:57.494: E/AndroidRuntime(736):  at android.widget.Toast.<init>(Toast.java:92)
03-18 20:07:57.494: E/AndroidRuntime(736):  at android.widget.Toast.makeText(Toast.java:238)
03-18 20:07:57.494: E/AndroidRuntime(736):  at one.example.sqlitetesting.Message.<init>(Message.java:9)
03-18 20:07:57.494: E/AndroidRuntime(736):  at one.example.sqlitetesting.DbHelper.<init>(DbHelper.java:19)
03-18 20:07:57.494: E/AndroidRuntime(736):  at one.example.sqlitetesting.MainActivity.onCreate(MainActivity.java:14)
03-18 20:07:57.494: E/AndroidRuntime(736):  at android.app.Activity.performCreate(Activity.java:5008)
03-18 20:07:57.494: E/AndroidRuntime(736):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-18 20:07:57.494: E/AndroidRuntime(736):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-18 20:07:57.494: E/AndroidRuntime(736):  ... 11 more
03-18 20:08:01.104: I/Process(736): Sending signal. PID: 736 SIG: 9`

这是Mainactivity类:

package one.example.sqlitetesting;

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

public class MainActivity extends Activity {

DbHelper X;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    X=new DbHelper(this);
}

@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;
}

}

这是DBhelper类:

package one.example.sqlitetesting;

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

public class DbHelper extends SQLiteOpenHelper {

static String AKHIL_DATABASE="mydatabase";
String TABLE_NAME="usersdata";
static int version=1;
Context context;

//this is a super class constructor which 
public DbHelper(Context context) {
    super(context, AKHIL_DATABASE, null, version);//parameters: context,name of DB, custom cursor(null:defaul),version
    context=this.context;
    Message m=new Message(context, "constructer called");
}



@Override
public void onCreate(SQLiteDatabase db) {

    //create table usersdata (_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(255));
    String query="create table "+TABLE_NAME+" usersdata (_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(255));";
    try {
        db.execSQL(query);
        Message m=new Message(context, "onccreate is called");
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        Message m=new Message(context, ""+e);
    }
}


@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {

    try {
        db.execSQL("drop table userdata if exists;");
        onCreate(db);
    } catch (SQLException e) {
        // TODO Auto-generated catch block

    }

}

}

这是Message类,它在不同级别提供toast消息:     package one.example.sqlitetesting;

import android.content.Context;
import android.widget.Toast;

public class Message {
public Message(Context context,String message)
{
    Toast.makeText(context, message, Toast.LENGTH_LONG).show();
}
}

1 个答案:

答案 0 :(得分:4)

在您的DBHelper()中,您有context=this.context;行应该是this.context = context

您正在使用对象context覆盖context参数,该对象当时未初始化。因此,我希望您contextnull