我有这段代码,其中向用户显示活动布局,让他添加姓名和电子邮件,然后单击按钮进行订阅。程序唯一能做的就是将详细信息存储在表中。我的程序中没有任何语法错误,应用程序已成功debubs,但是当我在4.4.4 Android版本上运行时,它会崩溃。 有什么建议吗?
DataHandler.java
package com.example.buttondemo;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DataHandler {
public static final String NAME = "name";
public static final String EMAIL = "email";
public static final String Table_name = "subcriptions";
public static final String Database_name = "visualDictionaryDB";
public static final int Database_Version = 1;
public static final String Table_create = "create table subcriptions (name text not null, email text not null)";
DatabaseHelper dbhelper;
Context ctx;
SQLiteDatabase db;
public DataHandler(Context ctx) {
// TODO Auto-generated constructor stub
this.ctx = ctx; //initialize context variable
dbhelper = new DatabaseHelper(ctx);
}
private static class DatabaseHelper extends SQLiteOpenHelper{
//initialize the database constructors to be called
public DatabaseHelper(Context ctx)
{
super(ctx, Database_name, null, Database_Version);
}
@Override
//used for creating the table in the DB
public void onCreate(SQLiteDatabase db) {
// Statements for creating the table using the string in the constructor
try
{
db.execSQL(Table_create);
}
catch(SQLException e)
{
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS subcriptions");
onCreate(db);
}
}
//method used for opening the db
public DataHandler open()
{
db = dbhelper.getWritableDatabase();
return this;
}
//method used for closing the db
public void close()
{
dbhelper.close();
}
//method used for inserting data into the db
public long insertData(String name, String email)
{
ContentValues content = new ContentValues();
content.put(NAME, name);
content.put(EMAIL, email);
return db.insert(Table_name, null, content);
}
//retrieving all the values from the DB
public Cursor returnData()
{
return db.query(Table_name, new String[] {NAME,EMAIL}, null, null, null, null, null);
}
}
MainActivity.java
public class MainActivity extends ActionBarActivity {
//initialize variables to be used many times
private final String TAG = "DemoButtonApp";
Button btnSubscribe;
EditText name,email;
DataHandler handler; //create object of datahandler
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//buttons for subscription
btnSubscribe = (Button) findViewById(R.id.btnSubscription);
name = (EditText) findViewById(R.id.Name);
email = (EditText) findViewById(R.id.Email);
btnSubscribe.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String getName = name.getText().toString();
String getEmail = email.getText().toString();
handler = new DataHandler(getBaseContext()); //passing data of the context class
handler.open();
long id = handler.insertData(getName, getEmail); // method defined below
Toast.makeText(getBaseContext(), "DATA INSERTED!", Toast.LENGTH_LONG).show();
handler.close();
}
});
显示上次Logcat错误
01-09 15:09:31.805: D/ActivityThread(14461): setTargetHeapMinFree:2097152
01-09 15:09:31.995: D/AndroidRuntime(14461): Shutting down VM
01-09 15:09:31.995: W/dalvikvm(14461): threadid=1: thread exiting with uncaught exception (group=0x4160dce0)
01-09 15:09:31.995: E/AndroidRuntime(14461): FATAL EXCEPTION: main
01-09 15:09:31.995: E/AndroidRuntime(14461): Process: com.example.buttondemo, PID: 14461
01-09 15:09:31.995: E/AndroidRuntime(14461): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.buttondemo/com.example.buttondemo.MainActivity}: java.lang.NullPointerException
01-09 15:09:31.995: E/AndroidRuntime(14461): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
01-09 15:09:31.995: E/AndroidRuntime(14461): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
01-09 15:09:31.995: E/AndroidRuntime(14461): at android.app.ActivityThread.access$800(ActivityThread.java:144)
01-09 15:09:31.995: E/AndroidRuntime(14461): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
01-09 15:09:31.995: E/AndroidRuntime(14461): at android.os.Handler.dispatchMessage(Handler.java:102)
01-09 15:09:31.995: E/AndroidRuntime(14461): at android.os.Looper.loop(Looper.java:136)
01-09 15:09:31.995: E/AndroidRuntime(14461): at android.app.ActivityThread.main(ActivityThread.java:5146)
01-09 15:09:31.995: E/AndroidRuntime(14461): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 15:09:31.995: E/AndroidRuntime(14461): at java.lang.reflect.Method.invoke(Method.java:515)
01-09 15:09:31.995: E/AndroidRuntime(14461): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
01-09 15:09:31.995: E/AndroidRuntime(14461): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
01-09 15:09:31.995: E/AndroidRuntime(14461): at dalvik.system.NativeStart.main(Native Method)
01-09 15:09:31.995: E/AndroidRuntime(14461): Caused by: java.lang.NullPointerException
01-09 15:09:31.995: E/AndroidRuntime(14461): at com.example.buttondemo.MainActivity.onCreate(MainActivity.java:31)
01-09 15:09:31.995: E/AndroidRuntime(14461): at android.app.Activity.performCreate(Activity.java:5231)
01-09 15:09:31.995: E/AndroidRuntime(14461): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-09 15:09:31.995: E/AndroidRuntime(14461): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
01-09 15:09:31.995: E/AndroidRuntime(14461): ... 11 more
01-09 15:19:40.624: D/ActivityThread(15095): handleBindApplication:com.example.buttondemo
01-09 15:19:40.624: D/ActivityThread(15095): setTargetHeapUtilization:0.75
01-09 15:19:40.624: D/ActivityThread(15095): setTargetHeapMinFree:2097152
01-09 15:19:40.754: D/AndroidRuntime(15095): Shutting down VM
01-09 15:20:12.028: D/ActivityThread(15187): handleBindApplication:com.example.buttondemo
01-09 15:20:12.028: D/ActivityThread(15187): setTargetHeapUtilization:0.75
01-09 15:20:12.028: D/ActivityThread(15187): setTargetHeapMinFree:2097152
01-09 15:20:12.188: D/AndroidRuntime(15187): Shutting down VM
01-09 15:20:12.188: W/dalvikvm(15187): threadid=1: thread exiting with uncaught exception (group=0x4160dce0)
01-09 15:20:12.188: E/AndroidRuntime(15187): FATAL EXCEPTION: main
01-09 15:20:12.188: E/AndroidRuntime(15187): Process: com.example.buttondemo, PID: 15187
01-09 15:20:12.188: E/AndroidRuntime(15187): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.buttondemo/com.example.buttondemo.MainActivity}: java.lang.NullPointerException
01-09 15:20:12.188: E/AndroidRuntime(15187): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
01-09 15:20:12.188: E/AndroidRuntime(15187): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
01-09 15:20:12.188: E/AndroidRuntime(15187): at android.app.ActivityThread.access$800(ActivityThread.java:144)
01-09 15:20:12.188: E/AndroidRuntime(15187): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
01-09 15:20:12.188: E/AndroidRuntime(15187): at android.os.Handler.dispatchMessage(Handler.java:102)
01-09 15:20:12.188: E/AndroidRuntime(15187): at android.os.Looper.loop(Looper.java:136)
01-09 15:20:12.188: E/AndroidRuntime(15187): at android.app.ActivityThread.main(ActivityThread.java:5146)
01-09 15:20:12.188: E/AndroidRuntime(15187): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 15:20:12.188: E/AndroidRuntime(15187): at java.lang.reflect.Method.invoke(Method.java:515)
01-09 15:20:12.188: E/AndroidRuntime(15187): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
01-09 15:20:12.188: E/AndroidRuntime(15187): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
01-09 15:20:12.188: E/AndroidRuntime(15187): at dalvik.system.NativeStart.main(Native Method)
01-09 15:20:12.188: E/AndroidRuntime(15187): Caused by: java.lang.NullPointerException
01-09 15:20:12.188: E/AndroidRuntime(15187): at com.example.buttondemo.MainActivity.onCreate(MainActivity.java:31)
01-09 15:20:12.188: E/AndroidRuntime(15187): at android.app.Activity.performCreate(Activity.java:5231)
01-09 15:20:12.188: E/AndroidRuntime(15187): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-09 15:20:12.188: E/AndroidRuntime(15187): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
01-09 15:20:12.188: E/AndroidRuntime(15187): ... 11 more
01-09 15:21:48.090: I/Process(15187): Sending signal. PID: 15187 SIG: 9
01-09 15:21:48.250: D/ActivityThread(15302): handleBindApplication:com.example.buttondemo
01-09 15:21:48.250: D/ActivityThread(15302): setTargetHeapUtilization:0.75
01-09 15:21:48.250: D/ActivityThread(15302): setTargetHeapMinFree:2097152
01-09 15:21:48.391: D/AndroidRuntime(15302): Shutting down VM
01-09 15:21:48.391: W/dalvikvm(15302): threadid=1: thread exiting with uncaught exception (group=0x4160dce0)
01-09 15:21:48.391: E/AndroidRuntime(15302): FATAL EXCEPTION: main
01-09 15:21:48.391: E/AndroidRuntime(15302): Process: com.example.buttondemo, PID: 15302
01-09 15:21:48.391: E/AndroidRuntime(15302): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.buttondemo/com.example.buttondemo.MainActivity}: java.lang.NullPointerException
01-09 15:21:48.391: E/AndroidRuntime(15302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
01-09 15:21:48.391: E/AndroidRuntime(15302): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
01-09 15:21:48.391: E/AndroidRuntime(15302): at android.app.ActivityThread.access$800(ActivityThread.java:144)
01-09 15:21:48.391: E/AndroidRuntime(15302): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
01-09 15:21:48.391: E/AndroidRuntime(15302): at android.os.Handler.dispatchMessage(Handler.java:102)
01-09 15:21:48.391: E/AndroidRuntime(15302): at android.os.Looper.loop(Looper.java:136)
01-09 15:21:48.391: E/AndroidRuntime(15302): at android.app.ActivityThread.main(ActivityThread.java:5146)
01-09 15:21:48.391: E/AndroidRuntime(15302): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 15:21:48.391: E/AndroidRuntime(15302): at java.lang.reflect.Method.invoke(Method.java:515)
01-09 15:21:48.391: E/AndroidRuntime(15302): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
01-09 15:21:48.391: E/AndroidRuntime(15302): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
01-09 15:21:48.391: E/AndroidRuntime(15302): at dalvik.system.NativeStart.main(Native Method)
01-09 15:21:48.391: E/AndroidRuntime(15302): Caused by: java.lang.NullPointerException
01-09 15:21:48.391: E/AndroidRuntime(15302): at com.example.buttondemo.MainActivity.onCreate(MainActivity.java:31)
01-09 15:21:48.391: E/AndroidRuntime(15302): at android.app.Activity.performCreate(Activity.java:5231)
01-09 15:21:48.391: E/AndroidRuntime(15302): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-09 15:21:48.391: E/AndroidRuntime(15302): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
01-09 15:21:48.391: E/AndroidRuntime(15302): ... 11 more
01-09 15:21:50.303: I/Process(15302): Sending signal. PID: 15302 SIG: 9
01-09 15:22:41.377: D/ActivityThread(15369): handleBindApplication:com.example.buttondemo
01-09 15:22:41.377: D/ActivityThread(15369): setTargetHeapUtilization:0.75
01-09 15:22:41.377: D/ActivityThread(15369): setTargetHeapMinFree:2097152
01-09 15:22:41.527: D/AndroidRuntime(15369): Shutting down VM
01-09 15:22:41.527: W/dalvikvm(15369): threadid=1: thread exiting with uncaught exception (group=0x4160dce0)
01-09 15:22:41.527: E/AndroidRuntime(15369): FATAL EXCEPTION: main
01-09 15:22:41.527: E/AndroidRuntime(15369): Process: com.example.buttondemo, PID: 15369
01-09 15:22:41.527: E/AndroidRuntime(15369): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.buttondemo/com.example.buttondemo.MainActivity}: java.lang.NullPointerException
01-09 15:22:41.527: E/AndroidRuntime(15369): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
01-09 15:22:41.527: E/AndroidRuntime(15369): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
01-09 15:22:41.527: E/AndroidRuntime(15369): at android.app.ActivityThread.access$800(ActivityThread.java:144)
01-09 15:22:41.527: E/AndroidRuntime(15369): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
01-09 15:22:41.527: E/AndroidRuntime(15369): at android.os.Handler.dispatchMessage(Handler.java:102)
01-09 15:22:41.527: E/AndroidRuntime(15369): at android.os.Looper.loop(Looper.java:136)
01-09 15:22:41.527: E/AndroidRuntime(15369): at android.app.ActivityThread.main(ActivityThread.java:5146)
01-09 15:22:41.527: E/AndroidRuntime(15369): at java.lang.reflect.Method.invokeNative(Native Method)
01-09 15:22:41.527: E/AndroidRuntime(15369): at java.lang.reflect.Method.invoke(Method.java:515)
01-09 15:22:41.527: E/AndroidRuntime(15369): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
01-09 15:22:41.527: E/AndroidRuntime(15369): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
01-09 15:22:41.527: E/AndroidRuntime(15369): at dalvik.system.NativeStart.main(Native Method)
01-09 15:22:41.527: E/AndroidRuntime(15369): Caused by: java.lang.NullPointerException
01-09 15:22:41.527: E/AndroidRuntime(15369): at com.example.buttondemo.MainActivity.onCreate(MainActivity.java:31)
01-09 15:22:41.527: E/AndroidRuntime(15369): at android.app.Activity.performCreate(Activity.java:5231)
01-09 15:22:41.527: E/AndroidRuntime(15369): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-09 15:22:41.527: E/AndroidRuntime(15369): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
01-09 15:22:41.527: E/AndroidRuntime(15369): ... 11 more