0

내 응용 프로그램 내에 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 (목록보기 나의 활동)

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

'Game.java'의 40 행은 무엇입니까? – Emmanuel

+0

줄에 SQLiteOpenHelper.java:224가 있는지 알려줄 수 있습니까? 디버깅과 로그 캣은 친구입니다 – anotherBug

+0

Emmanuel - 40 행 : ListviewGamesAdapter adapter = new ListviewGamesAdapter (getActivity(), db.getAllGames()); – Bloodyman2103

답변

0

데이터베이스 인스턴스를 가져 오기 위해서는 컨텍스트가 필요하지만 DBHelper null의 컨스트럭터를 전달해야합니다. 적어도 onCreate까지 DBHelper를 초기화하면 안됩니다. 이 대신 :

DBHelper db = new DBHelper(getActivity()); 

이 작업을 수행 : 또한

DBHelper db; 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 

    db = new DBHelper(getActivity()); 

    //... 
} 

, 당신이 제대로 커서를 액세스하지 않습니다 생각합니다. 예를 들어, 열 1이 TEXT이기 때문에 cursor.getBlob(1)은 널 (NULL)을 리턴해야합니다. 또한 열 4가 DATE이므로 cursor.getText(4)은 널 (NULL)을 리턴해야합니다. 또한 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); 
+0

그리고 정확히 어디에 넣어야합니까? 다음과 같이 편집해야합니다 : Game game = new Game (String title = cursor.getString (cursor.getColumnIndex (FLD_TITLE)), String description = etc ...) – Bloodyman2103

+0

편집 내 대답 – ashishduh

+0

오류가 계속 발생하지만이 버그를보고 해 주셔서 감사합니다! :) – Bloodyman2103

관련 문제