這是錯誤:爲什麼我在這段代碼中得到一個NullPointerException
07-14 16:13:53.385 19921-19921/studentzone.studentzone E/AndroidRuntime: FATAL EXCEPTION: main
Process: studentzone.studentzone, PID: 19921
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:4444)
at android.view.View$PerformClick.run(View.java:18457)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5113)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4444)
at android.view.View$PerformClick.run(View.java:18457)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5113)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: near "tableuser": syntax error (code 1): , while compiling: create tableuser(abctextemailemailtextemail12345text)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
at studentzone.studentzone.DataBaseHelper.onCreate(DataBaseHelper.java:30)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at studentzone.studentzone.DataBaseHelper.insertData(DataBaseHelper.java:43)
at studentzone.studentzone.SignUp.register(SignUp.java:31)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:4444)
at android.view.View$PerformClick.run(View.java:18457)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5113)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
我的數據庫類是:
public class DataBaseHelper extends SQLiteOpenHelper {
public static final String Database_name="studentzone.db";
public static final String Table_name ="user";
public static final String Type ="text";
public static final String comma ="email";
public static final String Name ="abc";
public static final String Email ="email";
public static final String Pass ="12345";
public static final String sql_create="create table"+Table_name+"("+Name+Type+comma+Email+Type+comma+Pass+Type+")";
public static final String sql_delete="drop table if exists" + DataBaseHelper.Table_name;
public DataBaseHelper(Context context) {
super(context,Database_name, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(sql_create);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL(sql_delete);
onCreate(db);
}
public Boolean insertData(String name , String email , String password)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(Name,name);
contentValues.put(Email,email);
contentValues.put(Pass,password);
long rslt =db.insert(Table_name,null,contentValues);
if(rslt==-1)
{
return false;
}
else
{
return true;
}
}
}
我的主類是:
public class SignUp extends AppCompatActivity {
DataBaseHelper Database;
Button login;
EditText editName, editEmail,editPassword;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
login= (Button) findViewById(R.id.button_login);
editName= (EditText) findViewById(R.id.edit_text_full_name);
editEmail= (EditText) findViewById(R.id.edit_text_email);
editPassword= (EditText) findViewById(R.id.edit_text_password);
Database=new DataBaseHelper(this);
}
public void register(View v)
{
Toast.makeText(SignUp.this,editName.getText().toString(), Toast.LENGTH_SHORT).show();
Toast.makeText(SignUp.this,editEmail.getText().toString(), Toast.LENGTH_SHORT).show();
Toast.makeText(SignUp.this,editPassword.getText().toString(), Toast.LENGTH_SHORT).show();
boolean isInserted=Database.insertData(editName.getText().toString(),editEmail.getText().toString(),editPassword.getText().toString());
if(isInserted==true)
{
Toast.makeText(getApplicationContext(),"value inserted",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(),"no value inserted",Toast.LENGTH_SHORT).show();
}
}
public void login(View v)
{
Intent intent = new Intent(SignUp.this,LoginActivity.class);
startActivity(intent);
}
}
您的堆棧跟蹤任何地方都不會顯示一個NullPointerException。它顯示了一個SQLiteException。 –