MainActivity.java:
public class MainActivity extends AppCompatActivity{
EditText addTitle, addDiary;
List<diaryList> lists = new ArrayList<diaryList>();
ListView diaryListView;
databaseHandler dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addTitle = (EditText) findViewById(R.id.addTitle);
addDiary = (EditText) findViewById(R.id.addDiary);
TabHost tabHost = (TabHost) findViewById(R.id.tabHost);
diaryListView = (ListView) findViewById(R.id.listView);
tabHost.setup();
TabHost.TabSpec tabSpec = tabHost.newTabSpec("Write New Diary");
tabSpec.setContent(R.id.write);
tabSpec.setIndicator("Write New Diary");
tabHost.addTab(tabSpec);
tabSpec = tabHost.newTabSpec("Saved Diary");
tabSpec.setContent(R.id.saved);
tabSpec.setIndicator("Saved Diary");
tabHost.addTab(tabSpec);
final Button saveDiary = (Button) findViewById(R.id.saveDiary);
saveDiary.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
diaryList dList = new diaryList(dbHandler.getDiaryCount(), String.valueOf(addTitle.getText()), String.valueOf(addDiary.getText()));
dbHandler.createDiary(dList);
lists.add(dList);
populateList();
Toast.makeText(getApplicationContext(), addTitle.getText().toString() + " has been created!", Toast.LENGTH_SHORT).show();
}
});
addTitle.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
saveDiary.setEnabled(!addTitle.getText().toString().trim().isEmpty());
}
@Override
public void afterTextChanged(Editable s) {
}
});
List<diaryList> addableDiary = dbHandler.getAllDiary();
int diaryCount = dbHandler.getDiaryCount();
for(int i=0; i<diaryCount; i++) {
lists.add(addableDiary.get(i));
}
if(!addableDiary.isEmpty())
populateList();
}
private void populateList(){
ArrayAdapter<diaryList> adapter = new diaryListAdpater();
diaryListView.setAdapter(adapter);
}
private void addNewDiary(String title, String content){
lists.add(new diaryList(0, title, content));
}
private class diaryListAdpater extends ArrayAdapter<diaryList>{
public diaryListAdpater(){
super (MainActivity.this, R.layout.listview, lists);
}
@Override
public View getView(int position, View view, ViewGroup parent){
if(view == null)
view = getLayoutInflater().inflate(R.layout.listview, parent, false);
diaryList currentDiary = lists.get(position);
TextView title = (TextView) view.findViewById(R.id.titleList);
title.setText(currentDiary.getTitle());
TextView content = (TextView) view.findViewById(R.id.contentList);
content.setText(currentDiary.getContent());
return view;
}
}
}
databasehandler.java:
public class databaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION =1;
private static final String DATABASE_NAME = "diaryManager",
TABLE_DIARY = "DIARY",
KEY_ID = "ID",
KEY_TITLE = "TITLE",
KEY_CONTENT = "CONTENT";
public databaseHandler(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate (SQLiteDatabase db){
db.execSQL("CREATE TABLE " + TABLE_DIARY + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_TITLE +" TEXT, " + KEY_CONTENT + " TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("DROP TABLE IF EXISTS " + TABLE_DIARY);
onCreate(db);
}
public void createDiary (diaryList dlist){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TITLE, dlist.getTitle());
values.put(KEY_CONTENT, dlist.getContent());
db.insert(TABLE_DIARY, null, values);
db.close();
}
public diaryList getDiary (int id){
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.query(TABLE_DIARY, new String[] { KEY_ID, KEY_TITLE, KEY_CONTENT},KEY_ID + "=?", new String[] { String.valueOf(id)},null,null,null);
if (cursor != null)
cursor.moveToFirst();
diaryList dList = new diaryList(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2));
db.close();
cursor.close();
return dList;
}
public void deleteDiary(diaryList dList){
SQLiteDatabase db = getWritableDatabase();
db.delete(TABLE_DIARY, KEY_ID + "=?", new String[] {String.valueOf(dList.getID())});
db.close();
}
public int getDiaryCount() {
SQLiteDatabase db = getReadableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_DIARY, null);
int count = cursor.getCount();
db.close();
cursor.close();
return count;
}
public int updateDiary(diaryList dList){
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_TITLE, dList.getTitle());
values.put(KEY_CONTENT, dList.getContent());
return db.update(TABLE_DIARY, values, KEY_ID + "=?", new String[] {String.valueOf(dList.getID())});
}
public List<diaryList> getAllDiary(){
ArrayList<diaryList> lists = new ArrayList<diaryList>();
SQLiteDatabase db = getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_DIARY, null);
if(cursor.moveToFirst()){
do {
diaryList dList = new diaryList(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2));
lists.add(dList);
}
while (cursor.moveToNext());
}
return lists;
}
}
我收到了这个错误:
java.lang.RuntimeException:无法启动活动 ComponentInfo {com.example.iam_j.diary / com.example.iam_j.diary.MainActivity}: java.lang.NullPointerException:尝试调用虚方法 'java.util.List com.example.iam_j.diary.databaseHandler.getAllDiary()' 在null对象引用上 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 在android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 引起:java.lang.NullPointerException:尝试调用虚方法'java.util.List com.example.iam_j.diary.databaseHandler.getAllDiary()'上的null 对象参考 在com.example.iam_j.diary.MainActivity.onCreate(MainActivity.java:81) 在android.app.Activity.performCreate(Activity.java:6664) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 在android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6077) at java.lang.reflect.Method.invoke(Native Method) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 申请已终止。
任何人都可以帮我解决一下吗?