请你能帮我解决错误“java.lang.NullPointerException”。我试图在数据库中保存一些数据(名称和电子邮件),然后加载它。当我尝试启动它时,我的应用程序在模拟器中崩溃。我找到了答案并碰到了这个帖子: Error java.lang.NullPointerException on method getReadableDatabase()但它没有解决问题。
MainActivity.java
package com.example.test;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
Button save, load;
EditText name, email;
DataHandler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
save = (Button)findViewById(R.id.save);
load = (Button)findViewById(R.id.load);
name = (EditText)findViewById(R.id.name);
email = (EditText)findViewById(R.id.email);
save.setOnTouchListener(new OnTouchListener(){
public void onClick(View v){
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
String getName = name.getText().toString();
String getEmail = email.getText().toString();
handler = new DataHandler(getBaseContext());
handler.open();
long id = handler.insertData(getName, getEmail);
Toast.makeText(getBaseContext(), "Data inserted", Toast.LENGTH_LONG).show();
handler.close();
return false;
}
});
load.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
String getName, getEmail;
getName = "";
getEmail = "";
handler = new DataHandler(getBaseContext());
handler.open();
Cursor C = handler.returnData();
if(C.moveToFirst())
{
do
{
getName = C.getString(0);
getEmail = C.getString(1);
}while(C.moveToNext());
}
handler.close();
Toast.makeText(getBaseContext(), "Name : "+getName+" and email : "+getEmail, Toast.LENGTH_LONG).show();
return false;
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
DataHandler.java
package com.example.test;
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.SQLiteDatabase.CursorFactory;
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 = "mytable";
public static final String DATA_BASE_NAME = "mydatabase";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_CREATE = "create table mytable (name text not null, email text not null);";
DataBaseHelper dbhelper;
private static Context ctx;
SQLiteDatabase db;
public DataHandler(Context ctx)
{
this.ctx = ctx;
dbhelper = new DataBaseHelper(ctx);
}
private static class DataBaseHelper extends SQLiteOpenHelper
{
public DataBaseHelper(Context ctx)
{
super(ctx, DATA_BASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try{
db.execSQL("TABLE_CREATE");
}
catch(SQLException e)
{
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS mytable");
onCreate(db);
}
}
public DataHandler open()
{
db = dbhelper.getWritableDatabase();
return this;
}
public void close()
{
dbhelper.close();
}
public long insertData(String name, String email)
{
ContentValues content = new ContentValues();
content.put(NAME, name);
content.put(EMAIL, email);
return db.insertOrThrow(TABLE_NAME, null, content);
}
public Cursor returnData()
{
return db.query(TABLE_NAME, new String[] {NAME, EMAIL}, null, null, null, null, null);
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
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="com.example.test.MainActivity"
tools:ignore="MergeRootFrame" >
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="36dp"
android:ems="10"
android:hint="Enter name here" >
<requestFocus />
</EditText>
<Button
android:id="@+id/load"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/save"
android:layout_marginBottom="81dp"
android:text="LOAD DATA" />
<EditText
android:id="@+id/email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/name"
android:layout_below="@+id/name"
android:layout_marginTop="41dp"
android:ems="10"
android:hint="Enter email here" />
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/load"
android:layout_centerHorizontal="true"
android:layout_marginBottom="42dp"
android:text="SAVE DATA" />
</RelativeLayout>
logcat的
05-11 14:09:06.363: E/AndroidRuntime(2259): FATAL EXCEPTION: main
05-11 14:09:06.363: E/AndroidRuntime(2259): Process: com.example.test, PID: 2259
05-11 14:09:06.363: E/AndroidRuntime(2259): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: java.lang.NullPointerException
05-11 14:09:06.363: E/AndroidRuntime(2259): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-11 14:09:06.363: E/AndroidRuntime(2259): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-11 14:09:06.363: E/AndroidRuntime(2259): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-11 14:09:06.363: E/AndroidRuntime(2259): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-11 14:09:06.363: E/AndroidRuntime(2259): at android.os.Handler.dispatchMessage(Handler.java:102)
05-11 14:09:06.363: E/AndroidRuntime(2259): at android.os.Looper.loop(Looper.java:136)
05-11 14:09:06.363: E/AndroidRuntime(2259): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-11 14:09:06.363: E/AndroidRuntime(2259): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 14:09:06.363: E/AndroidRuntime(2259): at java.lang.reflect.Method.invoke(Method.java:515)
05-11 14:09:06.363: E/AndroidRuntime(2259): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-11 14:09:06.363: E/AndroidRuntime(2259): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-11 14:09:06.363: E/AndroidRuntime(2259): at dalvik.system.NativeStart.main(Native Method)
05-11 14:09:06.363: E/AndroidRuntime(2259): Caused by: java.lang.NullPointerException
05-11 14:09:06.363: E/AndroidRuntime(2259): at com.example.test.MainActivity.onCreate(MainActivity.java:34)
05-11 14:09:06.363: E/AndroidRuntime(2259): at android.app.Activity.performCreate(Activity.java:5231)
05-11 14:09:06.363: E/AndroidRuntime(2259): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-11 14:09:06.363: E/AndroidRuntime(2259): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-11 14:09:06.363: E/AndroidRuntime(2259): ... 11 more
编辑后重新发布DataHandler.java
package com.example.test;
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.SQLiteDatabase.CursorFactory;
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 = "mytable";
public static final String DATA_BASE_NAME = "mydatabase";
public static final int DATABASE_VERSION = 1;
public static final String TABLE_CREATE = "create table mytable (name text not null, email text not null)";
DataBaseHelper dbhelper;
Context ctx;
SQLiteDatabase db;
public DataHandler(Context ctx)
{
this.ctx = ctx;
dbhelper = new DataBaseHelper(ctx);
}
private static class DataBaseHelper extends SQLiteOpenHelper
{
public DataBaseHelper(Context ctx)
{
super(ctx, DATA_BASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try{
db.execSQL(TABLE_CREATE);
}
catch(SQLException e)
{
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS mytable");
onCreate(db);
}
}
public DataHandler open()
{
db = dbhelper.getWritableDatabase();
return this;
}
public void close()
{
dbhelper.close();
}
public long insertData(String name, String email)
{
ContentValues content = new ContentValues();
content.put(NAME, name);
content.put(EMAIL, email);
return db.insertOrThrow(TABLE_NAME, null, content);
}
public Cursor returnData()
{
return db.query(TABLE_NAME, new String[] {NAME, EMAIL}, null, null, null, null, null);
}
}
答案 0 :(得分:1)
这似乎与SQLite无关。
您发布的跟踪信息表示错误发生在第34行:
save.setOnTouchListener(new OnTouchListener(){
因此,当您尝试应用该事件时,您的save
按钮必须为空。
检查第29行是否实际返回你和实例:
save = (Button)findViewById(R.id.save);
答案 1 :(得分:0)
请用这个..
save.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
String getName = name.getText().toString();
String getEmail = email.getText().toString();
handler = new DataHandler(getBaseContext());
handler.open();
long id = handler.insertData(getName, getEmail);
Toast.makeText(getBaseContext(), "Data inserted", Toast.LENGTH_LONG).show();
handler.close();
return false;
}
});
我觉得你的ontouch()似乎有问题。请把我的代码代替你的代码......可能是有效的..
答案 2 :(得分:0)
你所做的所有事情都做得很好,但在你onCreate()
的{{1}}内错过了一件小事。
DataHandler.java
并改变这一点,
@Override
public void onCreate(SQLiteDatabase db)
{
try{
//db.execSQL("TABLE_CREATE");
// Here you passing `TABLE_CREATE` string instead of passing `TABLE_CREATE` variable's string. So kindly remove quotes surrounded by 'TABLE_CREATE' and try.
db.execSQL(TABLE_CREATE);
}
catch(SQLException e)
{
e.printStackTrace();
}
}
到
public static final String TABLE_CREATE = "create table mytable (name text not null, email text not null);";