我的应用程序中有一个SQLite数据库,我需要从我的数据库中获取数据并将这些数据放入我自定义的列表视图中。这个列表视图位于我的游戏片段中,我用于我的Viewpager活动(所以你可以浏览活动)我为它编写了所有代码,但是当我运行我的应用程序时,我的应用程序一直崩溃并给我同样的崩溃报告。
如果有办法解决它会很棒。
碰撞报告:
05-19 11:07:38.096: E/AndroidRuntime(775): FATAL EXCEPTION: main
05-19 11:07:38.096: E/AndroidRuntime(775): java.lang.NullPointerException
05-19 11:07:38.096: E/AndroidRuntime(775): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
05-19 11:07:38.096: E/AndroidRuntime(775): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
05-19 11:07:38.096: E/AndroidRuntime(775): at com.example.gamenity.DBHelper.getAllGames(DBHelper.java:122)
05-19 11:07:38.096: E/AndroidRuntime(775): at com.example.gamenity.Games.onCreateView(Games.java:40)
05-19 11:07:38.096: E/AndroidRuntime(775): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
05-19 11:07:38.096: E/AndroidRuntime(775): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
05-19 11:07:38.096: E/AndroidRuntime(775): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
05-19 11:07:38.096: E/AndroidRuntime(775): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-19 11:07:38.096: E/AndroidRuntime(775): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
05-19 11:07:38.096: E/AndroidRuntime(775): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
05-19 11:07:38.096: E/AndroidRuntime(775): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
05-19 11:07:38.096: E/AndroidRuntime(775): at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
05-19 11:07:38.096: E/AndroidRuntime(775): at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
05-19 11:07:38.096: E/AndroidRuntime(775): at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
DBHelper.class
package com.example.gamenity;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
public class DBHelper extends SQLiteOpenHelper {
// database
private static final String DB_NAME = "GamenityDB";
private static final int DB_VERSION = 6;
// tabellen
private static final String TABLE_USERS = "Users";
private static final String TABLE_GAMES = "Games";
public static final String KEY_ID = "userID";
private static final String CREATE_USERS =
"create table Users(userID integer primary key autoincrement, "
+ "username text not null, password text not null, email text not null, gender text, DOB text, picture BLOB);";
public static final String FLD_USERNAME = "username";
public static final String FLD_PASSWORD = "password";
public static final String FLD_EMAIL = "email";
public static final String FLD_GENDER = "gender";
private static final String CREATE_GAMES =
"create table Games (gameID integer primary key autoincrement, "
+ "Title varchar(255) not null, Description text not null, Genre text not null, Release date null, PS4 text null, " +
"PS3 text null, X1 text null, X360 text null, WiiU text null, PC text null, gamePicture blob null);";
public static final String FLD_TITLE = "Title";
public static final String FLD_DESCRIPTION = "Description";
public static final String FLD_GENRE = "Genre";
public static final String FLD_RELEASE = "Release";
public static final String FLD_PS4 = "PS4";
public static final String FLD_PS3 = "PS3";
public static final String FLD_X1 = "X1";
public static final String FLD_X360 = "X360";
public static final String FLD_WiiU = "WiiU";
public static final String FLD_PC = "PC";
public static final String FLD_GAMEIMAGE = "gamePicture";
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_USERS);
db.execSQL(CREATE_GAMES);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_USERS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_GAMES);
onCreate(db);
}
// database functies
public void createUser(String naam, String pw, String mail, String gender) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(FLD_USERNAME, naam);
values.put(FLD_PASSWORD, pw);
values.put(FLD_EMAIL, mail);
values.put(FLD_GENDER, gender);
db.insert(TABLE_USERS, null, values);
db.close();
}
public void createGame(String title, String description,String genre, String release,
String PS4, String PS3, String X1, String X360, String WiiU, String PC, byte[] gamePicture) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(FLD_TITLE, title);
values.put(FLD_DESCRIPTION, description);
values.put(FLD_GENRE, genre);
values.put(FLD_RELEASE, release);
values.put(FLD_PS4, PS4);
values.put(FLD_PS3, PS3);
values.put(FLD_X1, X1);
values.put(FLD_X360, X360);
values.put(FLD_WiiU, WiiU);
values.put(FLD_PC, PC);
values.put(FLD_GAMEIMAGE, gamePicture);
db.insert(TABLE_GAMES, null, values);
db.close();
}
public ArrayList<Game> getAllGames() {
ArrayList<Game> ArrayGames = new ArrayList<Game>();
String selectQuery = "SELECT * FROM " + TABLE_GAMES + " ORDER BY Release";
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
Game Game = new Game(cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4),cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8),cursor.getString(9), cursor.getString(10), cursor.getBlob(1));
ArrayGames.add(Game);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return ArrayGames;
}
Game.class
package com.example.gamenity;
public class Game {
private String title, description, genre, release, PS3, PS4, X1, X360, WiiU, PC;
private byte[] image;
public Game(String title, String description, String genre, String release,
String PS3, String PS4, String X1, String X360, String WiiU, String PC, byte[] image ) {
super();
this.title = title;
this.description = description;
this.genre = genre;
this.release = release;
this.PS3 = PS3;
this.PS4 = PS4;
this.X1 = X1;
this.X360 = X360;
this.WiiU = WiiU;
this.PC = PC;
this.image = image;
}
//Getters
public String getTitle() {
return this.title;
}
public String getDescription() {
return this.description;
}
public String getRelease() {
return this.release;
}
public String getGenre() {
return this.genre;
}
public String getPS3() {
return this.PS3;
}
public String getPS4() {
return this.PS4;
}
public String getX1() {
return this.X1;
}
public String getX360() {
return this.X360;
}
public String getWiiU() {
return this.WiiU;
}
public String getPC() {
return this.PC;
}
public byte[] getImage() {
return this.image;
}
}
ListviewGamesAdapter.class(我的列表视图的自定义适配器)
package com.example.gamenity;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class ListviewGamesAdapter extends ArrayAdapter<Game>{
private final Context context;
private final ArrayList<Game> GamesArrayList;
public ListviewGamesAdapter(Context context, ArrayList<Game> GamesArrayList) {
super(context, R.layout.list_row_games, GamesArrayList);
this.context = context;
this.GamesArrayList = GamesArrayList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// 1. Create inflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// 2. Get rowView from inflater
View rowView = inflater.inflate(R.layout.list_row_games, parent, false);
// 3. Get the two text view from the rowView
TextView Title = (TextView) rowView.findViewById(R.id.title);
TextView Genre= (TextView) rowView.findViewById(R.id.genre);
TextView Description = (TextView) rowView.findViewById(R.id.description);
// 4. Set the text for textView
Title.setText(GamesArrayList.get(position).getTitle());
Description.setText(GamesArrayList.get(position).getDescription());
Genre.setText(GamesArrayList.get(position).getGenre());
// 5. return rowView
return rowView;
}
}
list_row_games.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/listview_selector"
android:orientation="horizontal"
android:padding="5dip" >
<!-- Left side Thumbnail image -->
<LinearLayout android:id="@+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:layout_alignParentLeft="true"
android:background="@drawable/stroke"
android:layout_marginRight="5dip">
<ImageView
android:id="@+id/list_image"
android:layout_width="75dp"
android:layout_height="85dp"
android:src="@drawable/no_picture"/>
</LinearLayout>
<!-- Title -->
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/thumbnail"
android:layout_toRightOf="@+id/thumbnail"
android:text="Title"
android:textColor="@color/black"
android:typeface="sans"
android:textSize="15dip"
android:textStyle="bold"/>
<!-- Genre -->
<TextView
android:id="@+id/genre"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/title"
android:layout_below="@+id/title"
android:text="Genre"
android:textColor="@color/midGray"
android:textSize="12dip" />
<!-- Description -->
<TextView
android:id="@+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/follow"
android:layout_alignLeft="@+id/title"
android:layout_below="@+id/genre"
android:layout_toLeftOf="@+id/follow"
android:text="Description"
android:textColor="@color/darkGray"
android:textSize="13dip" />
<!-- Release date -->
<TextView
android:id="@+id/release"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/title"
android:layout_alignBottom="@+id/title"
android:layout_alignRight="@+id/genre"
android:gravity="right"
android:text="Fall 2014"
android:textColor="@color/midBlue"
android:textSize="10dip"
android:textStyle="bold" />
<Button
android:id="@+id/follow"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/thumbnail"
android:layout_alignRight="@+id/genre"
android:background="@drawable/button_follow"
android:drawableRight="@drawable/portal_stickman"
android:text="Follow"
android:textColor="@color/white"
android:textSize="15dp"
android:textStyle="bold" />
</RelativeLayout>
Games.class(我使用listview的活动)
package com.example.gamenity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
public class Games extends Fragment {
DBHelper db = new DBHelper(getActivity());
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
if(container == null) {
return null;
}
RelativeLayout mRelativeLayout = (RelativeLayout)inflater.inflate(R.layout.activity_games, container, false);
Button btn_addGame = (Button)mRelativeLayout.findViewById(R.id.addGame);
ListView list_games = (ListView)mRelativeLayout.findViewById(R.id.listviewGames);
btn_addGame.setOnClickListener(new View.OnClickListener() {
public void onClick (View view) {
Intent intent_addGame = new Intent(getActivity(), AddGame.class);
startActivity(intent_addGame);
}
});
ListviewGamesAdapter adapter = new ListviewGamesAdapter(getActivity(), db.getAllGames());
list_games.setAdapter(adapter);
db.close();
return mRelativeLayout;
}
}
Viewpager.class
package com.example.gamenity;
import java.util.List;
import java.util.Vector;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
public class Viewpager extends FragmentActivity {
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewpager);
initialisePaging();
ActionbarButtons();
}
private void initialisePaging() {
List<Fragment> pages = new Vector<Fragment>();
pages.add(Fragment.instantiate(this, Home.class.getName()));
pages.add(Fragment.instantiate(this, News.class.getName()));
pages.add(Fragment.instantiate(this, Games.class.getName()));
mPagerAdapter = new PagerAdapter(this.getSupportFragmentManager(), pages);
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(mPagerAdapter);
}
public void ActionbarButtons() {
ImageButton notifications_btn = (ImageButton)findViewById(R.id.notifications_button);
notifications_btn.setOnClickListener(new View.OnClickListener() {
public void onClick (View view) {
Intent notifications = new Intent(Viewpager.this, Notifications.class);
startActivity(notifications);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.viewpager, menu);
return true;
}
}
答案 0 :(得分:0)
您需要一个上下文才能获取数据库实例,但是您传递了DBHelper null的contstructor。您不应该至少在onCreate
之前初始化DBHelper。而不是:
DBHelper db = new DBHelper(getActivity());
这样做:
DBHelper db;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
db = new DBHelper(getActivity());
//...
}
另外,我相信您无法正确访问光标。例如,cursor.getBlob(1)
应该返回null,因为第1列是TEXT。此外,cursor.getText(4)
应返回null,因为第4列是DATE。此外,您无法在SQLite中存储日期,您应该将它们存储为很长的毫秒数。做这样的事情:
String title = cursor.getString(cursor.getColumnIndex(FLD_TITLE));
String customer = cursor.getString(cursor.getColumnIndex(FLD_DESCRIPTION));
Date date = (Date)cursor.getLong(cursor.getColumnIndex(FLD_RELEASE));
byte[] image = cursor.getBlob(cursor.getColumnIndex(FLD_GAMEIMAGE));
//...
Game Game = new Game(title, customer, date, //etc);
ArrayGames.add(Game);