我正在使用android studio进行android编程。关于ListView SQLite 适配器
我的项目在android stduo中没有错误。当我在模拟器中启动我的应用程序。 它没有开始,“不幸的是,应用已停止” 和SIG 9 ..
我的主要来源是。
public class MainActivity extends Activity {
final static int BOOK_ADD = 0;
final static int BOOK_INFO = 1;
ArrayList<Book> bookItem;
MyListAdapter Myadapter;
ListView BookList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DbOpenHelper db =new DbOpenHelper(this);
List<Book> mbook = db.getAllBooks();
for(Book bk : mbook){
bookItem.add(bk);
}
Myadapter = new MyListAdapter(this, R.layout.icontext , bookItem);
BookList = (ListView) findViewById(R.id.list);
BookList.setAdapter(Myadapter);
BookList.setOnItemClickListener(bItemClickListener);
}
OnItemClickListener bItemClickListener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Book item = (Book) parent.getItemAtPosition(position);
Intent intent = new Intent(getApplicationContext(), BookinfoActivity.class);
intent.putExtra("bookObject", item);
startActivityForResult(intent, BOOK_INFO);
}
};
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbarmenu, menu);
return super.onCreateOptionsMenu(menu);
}
public void mOnClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.addbook:
Book bookObject = new Book();
Intent intent = new Intent(MainActivity.this, AddBookActivity.class);
intent.putExtra("bookObject", bookObject);
startActivityForResult(intent, BOOK_ADD);
break;
case android.R.id.home:
break;
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case BOOK_ADD:
if (resultCode == RESULT_OK) {
Book newBook = (Book) data.getSerializableExtra("newBook");
DbOpenHelper db =new DbOpenHelper(this);
db.addBook(newBook);
bookItem.add(newBook);
Myadapter.notifyDataSetChanged();
break;
}
case BOOK_INFO:
if (resultCode == RESULT_OK) {
Book newBook = (Book) data.getSerializableExtra("newBook");
}
break;
}
}
protected void onDestroy() {
super.onDestroy();
}
}
class MyListAdapter extends ArrayAdapter<Book> {
private Context context;
private ArrayList<Book> arSrc = new ArrayList<Book>();
private int layoutResourceId;
public MyListAdapter(Context context, int layoutResourceId, ArrayList<Book> data) {
super(context,layoutResourceId,data);
this.context= context;
this.arSrc=data;
this.layoutResourceId = layoutResourceId;
}
public int getCount() {
return arSrc.size();
}
public Book getItem(int position) {
return arSrc.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
BookHolder holder;
if (row == null) {
LayoutInflater inflater =((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId,parent,false);
holder = new BookHolder();
holder.bname = (TextView)row.findViewById(R.id.bname);
holder.imgIcon =(ImageView)row.findViewById(img);
row.setTag(holder);
} else {
holder = (BookHolder)row.getTag();
}
Book bbb = arSrc.get(position);
holder.bname.setText(bbb.getBname());
holder.author.setText(bbb.getAuthor());
holder.publisher.setText(bbb.getPublisher());
holder.page = bbb.getPage();
holder.read = bbb.getRead();
byte[] outImage = bbb.getIcon();
ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
Bitmap theImage = BitmapFactory.decodeStream(imageStream);
holder.imgIcon.setImageBitmap(theImage);
//img.setImageBitmap(arSrc.get(position).getIcon().getDrawingCache());
//img.setImageBitmap(ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(bbb.getPath()),96,96));
//img.setImageResource(R.drawable.ic_add_circle_outline_black_24dp);
ProgressBar mProgBar = (ProgressBar)convertView.findViewById(R.id.prog);
mProgBar.setMax(arSrc.get(position).getPage());
mProgBar.setProgress(arSrc.get(position).getRead());
return row;
}
static class BookHolder{
ImageView imgIcon;
TextView bname,publisher,author;
int page,read;
}
}
和DbOpenHelper.java
public class DbOpenHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "Book.db";
private static final int DATABASE_VERSION = 1;
public DbOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(DataBase.CreateDB._CREATE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//table 변경 시시
db.execSQL("DROP TABLE IF EXISTS " + DataBase.CreateDB._TABLENAME);
onCreate(db);
}
public void addBook(Book book){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DataBase.CreateDB.NAME, book.getBname());
values.put(DataBase.CreateDB.PUBLISHER, book.getPublisher());
values.put(DataBase.CreateDB.AUTHOR, book.getAuthor());
values.put(DataBase.CreateDB.PAGE, book.getPage());
values.put(DataBase.CreateDB.READ, book.getRead());
values.put(DataBase.CreateDB.ICON, book.getIcon());
db.insert(DataBase.CreateDB._TABLENAME,null,values);
db.close();
}
Book getBook(int id){
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(DataBase.CreateDB._TABLENAME,new String[] {
DataBase.CreateDB._ID,
DataBase.CreateDB.NAME,
DataBase.CreateDB.PUBLISHER,
DataBase.CreateDB.AUTHOR,
DataBase.CreateDB.PAGE,
DataBase.CreateDB.READ,
DataBase.CreateDB.ICON},
DataBase.CreateDB._ID+"=?",new String[]{ String.valueOf(id)},
null,null,null,null);
if (cursor != null)
cursor.moveToFirst();
Book book = new Book(Integer.parseInt(cursor.getString(0)),
cursor.getString(1),cursor.getString(2),cursor.getString(3),
Integer.parseInt(cursor.getString(4)),
Integer.parseInt(cursor.getString(5)),
cursor.getBlob(6));
cursor.close();
return book;
}
public List<Book> getAllBooks(){
List<Book> bookList = new ArrayList<Book>();
String selectQuery = "SELECT * FROM Book";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery,null);
if(cursor.moveToFirst()){
do{
Book book = new Book();
book.set_id(Integer.parseInt(cursor.getString(0)));
book.setBname(cursor.getString(1));
book.setPublisher(cursor.getString(2));
book.setAuthor(cursor.getString(3));
book.setPage(Integer.parseInt(cursor.getString(4)));
book.setRead(Integer.parseInt(cursor.getString(5)));
book.setIcon(cursor.getBlob(6));
bookList.add(book);
}while (cursor.moveToNext());
}
cursor.close();
db.close();
return bookList;
}
public void deleteBook(Book book) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(DataBase.CreateDB._TABLENAME, DataBase.CreateDB._ID + " = ?",
new String[] { String.valueOf(book.get_id()) });
db.close();
}
}
我注释了关于数据库的事情。同样的问题。
和我的日志
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kang.book, PID: 25110
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kang.book/com.example.kang.book.MainActivity}:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at com.example.kang.book.MyListAdapter.getCount(MainActivity.java:136)
at android.widget.ListView.setAdapter(ListView.java:491)
at com.example.kang.book.MainActivity.onCreate(MainActivity.java:50)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Application terminated.