请你能帮我解决“java.lang.NullPointerException”这个错误。我试图将一些数据(名称和电子邮件)保存在数据库中,然后加载它。当我尝试启动它时,我的应用程序在模拟器中崩溃。我寻找答案,碰到这个线程: Error java.lang.NullPointerException on method getReadableDatabase()但它并没有解决这个问题。SQLiteDatabase中的java.lang.NullPointerException错误Android
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);
}
}
我想这是'支架在onCreate()方法的'} missplace – Kedarnath
检查是否'activity_main'布局包含“保存”按钮。也可以在'OnTouchListener'中使用'return true;'。最后,从'save.setOnTouchListener'中删除'public void onClick(View v){ }'' – SathishKumar
@SathishKumar这有助于启动应用程序。但只要输入姓名和电子邮件并点击保存,它就会崩溃。在LogCat上它说 05-12 00:19:47.393:E/AndroidRuntime(3048):android.database.sqlite.SQLiteException:没有这样的表:mytable(代码1):编译时:INSERT INTO mytable(email,name )VALUES(?,?) – rde