我創建了一個類和活動,只需要用戶填寫表單並將其存儲在數據庫中,但我收到空指針異常。我查了不少類似的帖子,但似乎沒有解決我的問題。請讓我知道我在這裏做錯了什麼。java.lang.runtimeexception:無法啓動活動componentinfo {}:java.lang.nullpointexception
非常感謝您的時間和考慮。
這裏是我的活動:
<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);
}
}
我認爲你沒有正確使用DBHelper。你應該有一個擴展dbhelper的類。看看教程。你沒有正確連接到你的db – CocoNess
@TanjaV感謝您的輸入。你能詳細說明一下,我沒有正確使用它,確切地說你的意思。我已經在上面添加了我的DbHelper類。謝謝! – Bix23