这是日志:
02-16 21:19:48.532: D/OpenGLRenderer(22826): Render dirty regions requested: true
02-16 21:19:48.538: D/Atlas(22826): Validating map...
02-16 21:19:48.648: I/Adreno-EGL(22826): <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/28/14, c33033c, Ia6306ec328
02-16 21:19:48.650: I/OpenGLRenderer(22826): Initialized EGL, version 1.4
02-16 21:19:48.674: D/OpenGLRenderer(22826): Enabling debug mode 0
02-16 21:19:56.921: D/AndroidRuntime(22826): Shutting down VM
02-16 21:19:56.925: E/AndroidRuntime(22826): FATAL EXCEPTION: main
02-16 21:19:56.925: E/AndroidRuntime(22826): Process: com.example.mythirdapp, PID: 22826
02-16 21:19:56.925: E/AndroidRuntime(22826): java.lang.IllegalStateException: Could not execute method of the activity
02-16 21:19:56.925: E/AndroidRuntime(22826): at android.view.View$1.onClick(View.java:4007)
02-16 21:19:56.925: E/AndroidRuntime(22826): at android.view.View.performClick(View.java:4756)
02-16 21:19:56.925: E/AndroidRuntime(22826): at android.view.View$PerformClick.run(View.java:19749)
02-16 21:19:56.925: E/AndroidRuntime(22826): at android.os.Handler.handleCallback(Handler.java:739)
02-16 21:19:56.925: E/AndroidRuntime(22826): at android.os.Handler.dispatchMessage(Handler.java:95)
02-16 21:19:56.925: E/AndroidRuntime(22826): at android.os.Looper.loop(Looper.java:135)
02-16 21:19:56.925: E/AndroidRuntime(22826): at android.app.ActivityThread.main(ActivityThread.java:5221)
02-16 21:19:56.925: E/AndroidRuntime(22826): at java.lang.reflect.Method.invoke(Native Method)
02-16 21:19:56.925: E/AndroidRuntime(22826): at java.lang.reflect.Method.invoke(Method.java:372)
02-16 21:19:56.925: E/AndroidRuntime(22826): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-16 21:19:56.925: E/AndroidRuntime(22826): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-16 21:19:56.925: E/AndroidRuntime(22826): Caused by: java.lang.reflect.InvocationTargetException
02-16 21:19:56.925: E/AndroidRuntime(22826): at java.lang.reflect.Method.invoke(Native Method)
02-16 21:19:56.925: E/AndroidRuntime(22826): at java.lang.reflect.Method.invoke(Method.java:372)
02-16 21:19:56.925: E/AndroidRuntime(22826): at android.view.View$1.onClick(View.java:4002)
02-16 21:19:56.925: E/AndroidRuntime(22826): ... 10 more
02-16 21:19:56.925: E/AndroidRuntime(22826): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'long com.example.edit.DatabaseClass.insertRecord(java.lang.String, java.lang.String, java.lang.String, java.lang.String)' on a null object reference
02-16 21:19:56.925: E/AndroidRuntime(22826): at com.example.edit.CreateActivity.save_send(CreateActivity.java:79)
02-16 21:19:56.925: E/AndroidRuntime(22826): ... 13 more
02-16 21:19:58.460: I/Process(22826): Sending signal. PID: 22826 SIG: 9
02-16 21:19:58.878: D/OpenGLRenderer(23137): Render dirty regions requested: true
02-16 21:19:58.885: D/Atlas(23137): Validating map...
02-16 21:19:58.924: I/Adreno-EGL(23137): <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/28/14, c33033c, Ia6306ec328
02-16 21:19:58.926: I/OpenGLRenderer(23137): Initialized EGL, version 1.4
02-16 21:19:58.959: D/OpenGLRenderer(23137): Enabling debug mode 0
数据库:
public class DatabaseClass extends SQLiteOpenHelper{
ContentValues initialValues = new ContentValues();
private SQLiteDatabase db;
private DatabaseClass dbHelper;
Context contextt;
public static final String KEY_ROWID = "id";
public static final String KEY_ITEMS="items";
public static final String KEY_DESCRIPTION="description";
public static final String KEY_DATE="date";
public static final String KEY_SEND_STATUS="send_status";
public static final String DB_NAME="g_shop";
public static final String DB_TABLE="shop_record";
public static final int DB_VERSION=1;
public static final String DB_CREATE="create table if not exists shop_record ("+
"ID int not null auto_increment,"+
"items varchar(255),"+
"description varchar(255),"+
"date datetime,"+
"send_status char(10),"+
"primary key (ID));";
public DatabaseClass(Context context, String name, CursorFactory factory,
int version) {
super(context, DB_NAME, null, DB_VERSION);
version = DB_VERSION;
factory=null;
name = DB_NAME;
context=contextt;
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DB_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public long insertRecord (String items, String description, String date, String send_status) {
initialValues.put(KEY_ITEMS, items);
initialValues.put(KEY_DESCRIPTION, description);
initialValues.put(KEY_DATE, date);
initialValues.put(KEY_SEND_STATUS, send_status);
return db.insert(DB_TABLE, KEY_DESCRIPTION, initialValues);
}
}
活动:
public class CreateActivity extends ActionBarActivity {
private SQLiteDatabase database;
private DatabaseClass dbHelper;
SimpleDateFormat s = new SimpleDateFormat("ddMMyyyyhhmmss");
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_create);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.create, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void save(View v){
EditText theitems = (EditText)findViewById(R.id.editText1);
EditText thedescription = (EditText)findViewById(R.id.editText2);
String items = theitems.getText().toString();
String description=thedescription.getText().toString();
String send_status="no";
String date = s.format(new Date());
}
public void save_send(View v){
EditText theitems = (EditText)findViewById(R.id.editText1);
EditText thedescription = (EditText)findViewById(R.id.editText2);
String items = theitems.getText().toString();
String description=thedescription.getText().toString();
String send_status="yes";
String date = s.format(new Date());
long success = dbHelper.insertRecord(items, description, date, send_status);
if (success == -1)
Toast.makeText(getApplicationContext(), "Not Inserted",Toast.LENGTH_LONG).show();
}
}
我对OOP相当新。我无法弄清楚为什么它是NPE,我已经初始化了struct中的变量,我将正确的格式传递给insertRecord方法。
答案 0 :(得分:0)
dbHelper
为空。它没有在构造函数中初始化,也从不在类中的任何地方填充。