0

I am making a small game for fun and I had everything working perfectly. Databases all displayed in textviews like they should have and everything was great. But I decided to add a Fragment View to have multiple pages (Instead of clicking buttons and show/hiding the information) And I thought everything was good. The pages swipe as they should but now the problem is that the information from the database doesn't display in the textview anymore...

I have included all code associated with the database:

private DBAdapter db;
private DBAdapter_Gold dbGold;

db = new DBAdapter(this.getActivity());
dbGold = new DBAdapter_Gold(this.getActivity());
dbGold.open();

try {
        String destPath = "/data/data/" + getActivity().getPackageName() + "/databases/GoldDB";
        File f = new File(destPath);
        if (!f.exists()) {
            CopyDB(getActivity().getBaseContext().getAssets().open("GoldDB"), new FileOutputStream(destPath));
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        dbGold.open();
        Cursor c = DBAdapter_Gold.getAllRecords();
        if (c.moveToFirst()) {
            do {
                DisplayRecordGold(c);
            } while (c.moveToNext());
        }

    } catch (Exception e) {
        Log.e("ERROR", "ERROR IN CODE:" + e.toString());
        e.printStackTrace();
    }


public void CopyDB(InputStream inputStream, OutputStream outputStream) throws IOException {
    // ---copy 1K bytes at a time---
    byte[] buffer = new byte[1024];
    int length;
    while ((length = inputStream.read(buffer)) > 0) {
        outputStream.write(buffer, 0, length);
    }
    inputStream.close();
    outputStream.close();
}

public void DisplayRecordGold(Cursor c) {
    TextView shopGoldValue1 = (TextView) getActivity().findViewById(R.id.shopGoldValue1);
    shopGoldValue1.setText(c.getString(1).toString());
    shopGoldValue1.setText("asdf");
}

Now I know that in order to get all the syntax errors to go away I had to add: getActivity() in front of a lot of things so maybe I did something wrong there. I've been staring at this far too long and could really use some help here..

Thanks.

Edit:

I should point out that the error is most likely in the DisplayRecord method. It seems as though it doesn't even call that method or like it doesn't find the variable to change the text. As you can see i set the text to "asdf" to see if it did anything and it just remained blank.

0

1 Answer 1

0

Change your code to:

db = new DBAdapter(this.getActivity().getApplication());
dbGold = new DBAdapter_Gold(this.getActivity().getApplication());
3
  • Did you try to set a breakpoint and debug your application to see which object is not getting the right value ?
    – spacebiker
    Dec 4, 2013 at 3:47
  • It keeps saying NullPointerException when calling the DisplayRecord method. So the problem is within the database. I have another activity that uses the same database and everything works just fine. the only difference is with the fragments made me change all my code to getActivity().* Dec 4, 2013 at 16:49
  • First of all, you should avoid opening the database twice, and i do not understand why are you copying the database manually, you should use SQLiteOpenHelper, but if you say it worked with activity, it should work also in the fragment. Anyway, i guess it's not a problem with the fragment but of the application design. Try extending your database class from SQLiteOpenHelper and let it do all the work for you. Or if you'd like to provide a database with data, you can take a look at SQLiteAssetHelper github.com/jgilfelt/android-sqlite-asset-helper
    – spacebiker
    Dec 4, 2013 at 18:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.