0

我创建了一个类和活动来简单地让用户填写表格并将其存储在数据库中,但我得到空指针异常。我查了很多类似的帖子,但似乎没有一个能解决我的问题。请让我知道我在这里做错了什么。

非常感谢您的时间和考虑。

这是我的活动:

    <RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Splash" 
><!--  android:background="@drawable/erc"-->

<EditText
    android:id="@+id/prayerRequestEditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="30dp"
    android:layout_marginTop="10dp"
    android:ems="10"
    android:hint="@string/prayer_request"
    android:inputType="textMultiLine" >
    <requestFocus />
</EditText>

<EditText
    android:id="@+id/prayForEditText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/prayerRequestEditText"
    android:layout_below="@+id/prayerRequestEditText"
    android:layout_marginTop="30dp"
    android:ems="10"
    android:hint="@string/pray_for" />

<TextView
    android:id="@+id/confidentialTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/prayForEditText"
    android:layout_below="@+id/prayForEditText"
    android:layout_marginTop="30dp"
    android:text="@string/confidential"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<Switch
    android:id="@+id/confidentialSwitch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/confidentialTextView"
    android:layout_alignRight="@+id/prayForEditText"
    android:textOff="@string/no"
    android:textOn="@string/yes" />

<TextView
    android:id="@+id/anonymousTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/confidentialTextView"
    android:layout_below="@+id/confidentialTextView"
    android:layout_marginTop="30dp"
    android:text="@string/anonymous"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<Switch
    android:id="@+id/anonymousSwitch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/anonymousTextView"
    android:layout_alignBottom="@+id/anonymousTextView"
    android:layout_alignRight="@+id/confidentialSwitch"
    android:textOff="@string/no"
    android:textOn="@string/yes" />

<Button
    android:id="@+id/sendRequestButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/anonymousTextView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:onClick="sendRequest"
    android:text="@string/send_request" />

这是我的课:

    package com.eastridgetoday.prayerrequest;
    import android.app.Activity;
    import android.content.ContentValues;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;

    public class PrayerRequest extends Activity{
SQLiteDatabase db;
DbHelper dbHelper;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);         
        setContentView(R.layout.activity_prayerrequest);
        dbHelper = new DbHelper(this);
        db = dbHelper.getWritableDatabase();        
        }

 public void sendRequest(View view){
     String prayers_request, pray_for, confidential, anonymous;

     EditText prayers_requestET = (EditText) view.findViewById(R.id.prayerRequestEditText);
     EditText pray_forET = (EditText) view.findViewById(R.id.prayForEditText);
     EditText confidentialET = (EditText) view.findViewById(R.id.confidentialSwitch);
     EditText anonymousET = (EditText) view.findViewById(R.id.anonymousSwitch);

     prayers_request = prayers_requestET.getText().toString();
     pray_for = pray_forET.getText().toString();
     confidential = confidentialET.getText().toString();
     anonymous = anonymousET.getText().toString();

     ContentValues cv = new ContentValues();
     cv.put(DbHelper.PRAYER_REQUEST, prayers_request);
     cv.put(DbHelper.PRAY_FOR, pray_for);
     cv.put(DbHelper.CONFIDENTIAL, confidential);
     cv.put(DbHelper.ANONYMOUS, anonymous);

     db.insert(DbHelper.TABLE_NAME, null, cv);
 }

}

我有一个启动活动作为启动活动,并且在显示启动 3 秒后应该会弹出祈祷请求表单。这是我的清单:

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

<uses-sdk
    android:minSdkVersion="14"
    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.eastridgetoday.prayerrequest.Splash"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.eastridgetoday.prayerrequest.PrayerRequest"
        android:label="@string/app_name" >

    </activity>
</application>

这是我的控制台:

    [2013-05-31 12:04:07 - EastridgePrayerRequest] ------------------------------
[2013-05-31 12:04:07 - EastridgePrayerRequest] Android Launch!
[2013-05-31 12:04:07 - EastridgePrayerRequest] adb is running normally.
[2013-05-31 12:04:07 - EastridgePrayerRequest] Performing     com.eastridgetoday.prayerrequest.Splash activity launch
[2013-05-31 12:04:07 - EastridgePrayerRequest] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'AVD_for_Nexus_S_by_Google'
[2013-05-31 12:04:07 - EastridgePrayerRequest] Uploading EastridgePrayerRequest.apk    onto device 'emulator-5554'
[2013-05-31 12:04:20 - EastridgePrayerRequest] Installing EastridgePrayerRequest.apk...
[2013-05-31 12:04:39 - EastridgePrayerRequest] Success!
[2013-05-31 12:04:39 - EastridgePrayerRequest] Starting activity     com.eastridgetoday.prayerrequest.Splash on device emulator-5554
[2013-05-31 12:04:43 - EastridgePrayerRequest] ActivityManager: Starting: Intent {       act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER]     cmp=com.eastridgetoday.prayerrequest/.Splash }

这是我的日志:

    05-31 01:16:45.353: E/Trace(3070): error opening trace file: No such file or directory (2)
   05-31 01:16:52.834: E/AndroidRuntime(3070): FATAL EXCEPTION: main
   05-31 01:16:52.834: E/AndroidRuntime(3070): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.eastridgetoday.prayerrequest/com.eastridgetoday.prayerrequest.PrayerRequest}: java.lang.NullPointerException
   05-31 01:16:52.834: E/AndroidRuntime(3070):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    05-31 01:16:52.834: E/AndroidRuntime(3070):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
   05-31 01:16:52.834: E/AndroidRuntime(3070):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
    05-31 01:16:52.834: E/AndroidRuntime(3070):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-31 01:16:52.834: E/AndroidRuntime(3070):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-31 01:16:52.834: E/AndroidRuntime(3070):     at android.os.Looper.loop(Looper.java:137)
05-31 01:16:52.834: E/AndroidRuntime(3070):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-31 01:16:52.834: E/AndroidRuntime(3070):     at java.lang.reflect.Method.invokeNative(Native Method)
05-31 01:16:52.834: E/AndroidRuntime(3070):     at java.lang.reflect.Method.invoke(Method.java:511)
05-31 01:16:52.834: E/AndroidRuntime(3070):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-31 01:16:52.834: E/AndroidRuntime(3070):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-31 01:16:52.834: E/AndroidRuntime(3070):     at dalvik.system.NativeStart.main(Native Method)
05-31 01:16:52.834: E/AndroidRuntime(3070): Caused by: java.lang.NullPointerException
05-31 01:16:52.834: E/AndroidRuntime(3070):     at com.eastridgetoday.prayerrequest.DbHelper.onCreate(DbHelper.java:35)
05-31 01:16:52.834: E/AndroidRuntime(3070):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
05-31 01:16:52.834: E/AndroidRuntime(3070):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
05-31 01:16:52.834: E/AndroidRuntime(3070):     at com.eastridgetoday.prayerrequest.PrayerRequest.onCreate(PrayerRequest.java:20)
05-31 01:16:52.834: E/AndroidRuntime(3070):     at android.app.Activity.performCreate(Activity.java:5104)
05-31 01:16:52.834: E/AndroidRuntime(3070):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
05-31 01:16:52.834: E/AndroidRuntime(3070):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
05-31 01:16:52.834: E/AndroidRuntime(3070):     ... 11 more

这是我的 DbHelper 类:

    package com.eastridgetoday.prayerrequest;
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;

    public class DbHelper extends SQLiteOpenHelper {

SQLiteDatabase db;

public static final String DATABASE_NAME = "prayersdb";
public static final String TABLE_NAME = "prayers_table";
public static final String P_ID = "_id";
public static final String PRAYER_REQUEST = "prayers";
public static final String PRAY_FOR = "pray_for";
public static final String CONFIDENTIAL = "confidential";
public static final String ANONYMOUS = "anonymous";
public static final int VERSION = 1;

public final String createTable = 
        "create table if not exists " + TABLE_NAME + " ( "
        + P_ID + " integer primary key autoincrement "
        + PRAYER_REQUEST + " text "
        + PRAY_FOR + " text "
        + CONFIDENTIAL +" text "
        + ANONYMOUS + " text);";

public DbHelper (Context context){
    super(context, DATABASE_NAME, null, VERSION);

}

@Override
public void onCreate(SQLiteDatabase arg0) {
    db.execSQL(createTable);

}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
    db.execSQL("drop table " + TABLE_NAME);

}

}

4

1 回答 1

1

问题出在 DbHelper 类中:

@Override
public void onCreate(SQLiteDatabase arg0) {
    db.execSQL(createTable);
}

db为空,你必须初始化它。

于 2013-05-31T21:38:19.887 回答