0

尝试了很多搜索,给出的解决方案是将活动添加到清单并将类添加到清单等等。没有得到任何解决方案。所以请帮忙。

按照在线教程学习 sqlite 数据库访问。尝试了以下并在 logcat 中得到了很多错误。

支持类 DbHelper

public class DbHelper extends SQLiteOpenHelper {

private static final String Name = "Record.db";
private static final int Version = 1;



public DbHelper(Context context){
    super(context,Name,null,Version);
}

@Override
public void onCreate(SQLiteDatabase db) {
    // TODO Auto-generated method stub

    db.execSQL("create table vijay(name text, id integer primary key)");
}

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

public void addRecord(String name, int id){
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put("name",name);
    values.put("id", id);

    db.insert("vijay", null, values);
    db.close();
}   
}

主要活动类

    public class MainActivity extends Activity {

    DbHelper dbhelper = new DbHelper(getApplicationContext());

    EditText et1;
    EditText et2;
    String s;
    int ss;


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

        et1 = (EditText)findViewById(R.id.editText1);
        et2 = (EditText)findViewById(R.id.editText2);
         s = et1.getText().toString();
         ss = Integer.parseInt(et2.getText().toString());
    }

    public void add(View v){

        dbhelper.addRecord(s,ss);
        et1.setText("");
        et2.setText("");
    }


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

安卓清单:

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

    <uses-sdk
        android:minSdkVersion="8"
        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.databaseaccess.MainActivity"
            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>

Logcat 显示很多错误

 07-29 16:48:56.793: E/AndroidRuntime(870): FATAL EXCEPTION: main
07-29 16:48:56.793: E/AndroidRuntime(870): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.databaseaccess/com.example.databaseaccess.MainActivity}: java.lang.NullPointerException
07-29 16:48:56.793: E/AndroidRuntime(870):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
07-29 16:48:56.793: E/AndroidRuntime(870):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-29 16:48:56.793: E/AndroidRuntime(870):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-29 16:48:56.793: E/AndroidRuntime(870):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-29 16:48:56.793: E/AndroidRuntime(870):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 16:48:56.793: E/AndroidRuntime(870):  at android.os.Looper.loop(Looper.java:123)
07-29 16:48:56.793: E/AndroidRuntime(870):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-29 16:48:56.793: E/AndroidRuntime(870):  at java.lang.reflect.Method.invokeNative(Native Method)
07-29 16:48:56.793: E/AndroidRuntime(870):  at java.lang.reflect.Method.invoke(Method.java:521)
07-29 16:48:56.793: E/AndroidRuntime(870):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-29 16:48:56.793: E/AndroidRuntime(870):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-29 16:48:56.793: E/AndroidRuntime(870):  at dalvik.system.NativeStart.main(Native Method)
07-29 16:48:56.793: E/AndroidRuntime(870): Caused by: java.lang.NullPointerException
07-29 16:48:56.793: E/AndroidRuntime(870):  at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100)
07-29 16:48:56.793: E/AndroidRuntime(870):  at com.example.databaseaccess.MainActivity.<init>(MainActivity.java:11)
07-29 16:48:56.793: E/AndroidRuntime(870):  at java.lang.Class.newInstanceImpl(Native Method)
07-29 16:48:56.793: E/AndroidRuntime(870):  at java.lang.Class.newInstance(Class.java:1429)
07-29 16:48:56.793: E/AndroidRuntime(870):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-29 16:48:56.793: E/AndroidRuntime(870):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
07-29 16:48:56.793: E/AndroidRuntime(870):  ... 11 more
4

3 回答 3

2

将以下行移动到您的OnCreate方法中

DbHelper dbhelper = new DbHelper(getApplicationContext());
于 2013-07-29T11:21:29.710 回答
0

只需为 DBHelper 创建全局变量....

像这样:

DbHelper dbhelper ;

然后在 onCreate() 方法中为它创建一个对象:

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

    dbhelper  = new DbHelper(MainActivity.this);


}
于 2013-07-29T11:25:49.063 回答
-1

我发现了另一个导致应用程序崩溃的错误。

  1. 正如 Shani 和 Abbas 提到的(感谢他们),在 onCreate 方法中实例化类。

  2. 在可以输入一些值之前,edittext 字段的值被存储到 onCreate 方法本身的 String 和 int 变量中。现在摆脱他们。将它们放在按钮的 onclick 事件函数中。

谢谢大家...

于 2013-07-29T11:35:01.227 回答