0

我一直在关注一个创建多个tablesin 的教程database,但是我遇到了一些错误并且没有成功解决它们。

这对我来说是新的,所以如果我犯了一些愚蠢的错误,请放轻松,但我想在这里学习。这些是classes我遇到错误的地方:

主要活动:

public class MainActivity extends FragmentActivity implements
    CustomPersonDialogFragment.CustomPersonDialogFragmentListener{

private Fragment contentFragment;
private PersonListFragment personListFragment;
private ContentFragment addPersonFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentManager fragmentManager = getSupportFragmentManager();

    CategoryDAO categoryDAO = new CategoryDAO(this);

    //Initially loads departments
    if (categoryDAO.getCategories().size() <= 0)
        categoryDAO.loadCategories();

    /*
     * This is called when orientation is changed.
     */
    if (savedInstanceState != null) {
        if (savedInstanceState.containsKey("content")) {
            String content = savedInstanceState.getString("content");
            if (content.equals(ContentFragment.ARG_ITEM_ID)) {
                if (fragmentManager
                        .findFragmentByTag(ContentFragment.ARG_ITEM_ID) != null) {
                    setFragmentTitle(R.string.add_person);
                    contentFragment = fragmentManager
                            .findFragmentByTag(ContentFragment.ARG_ITEM_ID);
                }
            }
        }
        if (fragmentManager.findFragmentByTag(PersonListFragment.ARG_ITEM_ID) != null) {
            personListFragment = (PersonListFragment) fragmentManager
                    .findFragmentByTag(PersonListFragment.ARG_ITEM_ID);
            contentFragment = personListFragment;
        }
    } else {
        personListFragment = new PersonListFragment();
        setFragmentTitle(R.string.app_name);
        switchContent(personListFragment, PersonListFragment.ARG_ITEM_ID);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_add:
            setFragmentTitle(R.string.add_person);
            addPersonFragment = new ContentFragment();
            switchContent(addPersonFragment, ContentFragment.ARG_ITEM_ID);

            return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
protected void onSaveInstanceState(Bundle outState) {
    if (contentFragment instanceof ContentFragment) {
        outState.putString("content", ContentFragment.ARG_ITEM_ID);
    } else {
        outState.putString("content", ContentFragment.ARG_ITEM_ID);
    }
    super.onSaveInstanceState(outState);
}

/*
 * We consider PersonListFragment as the home fragment and it is not added to
 * the back stack.
 */
public void switchContent(Fragment fragment, String tag) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    while (fragmentManager.popBackStackImmediate())
        ;

    if (fragment != null) {
        FragmentTransaction transaction = fragmentManager
                .beginTransaction();
        transaction.replace(R.id.content_frame, fragment, tag);
        // Only PersonAddFragment is added to the back stack.
        if (!(fragment instanceof PersonListFragment)) {
            transaction.addToBackStack(tag);
        }
        transaction.commit();
        contentFragment = fragment;
    }
}

protected void setFragmentTitle(int resourseId) {
    setTitle(resourseId);
    getActionBar().setTitle(resourseId);

}

/*
 * We call super.onBackPressed(); when the stack entry count is > 0. if it
 * is instanceof PersonListFragment or if the stack entry count is == 0, then
 * we prompt the user whether to quit the app or not by displaying dialog.
 * In other words, from PersonListFragment on back press it quits the app.
 */
@Override
public void onBackPressed() {
    FragmentManager fm = getSupportFragmentManager();
    if (fm.getBackStackEntryCount() > 0) {
        super.onBackPressed();
    } else if (contentFragment instanceof PersonListFragment
            || fm.getBackStackEntryCount() == 0) {
        //finish();
        //Shows an alert dialog on quit
        onShowQuitDialog();
    }
}

public void onShowQuitDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(false);

    builder.setMessage("Do You Want To Quit?");
    builder.setPositiveButton(android.R.string.yes,
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    finish();
                }
            });
    builder.setNegativeButton(android.R.string.no,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    builder.create().show();
}

/*
     * Callback used to communicate with PersonListFragment to notify the list adapter.
     * Communication between fragments goes via their Activity class.
     */
@Override
public void onFinishDialog() {
    if (personListFragment != null) {
        personListFragment.updateView();
    }
}
}

类别DAO:

public class CategoryDAO extends PersonDBDAO {

private static final String WHERE_ID_EQUALS = DataBaseHelper.ID_COLUMN
        + " =?";

public CategoryDAO(Context context) {
    super(context);
}

public long save(Category category) {
    ContentValues values = new ContentValues();
    values.put(DataBaseHelper.NAME_COLUMN, category.getName());

    return database.insert(DataBaseHelper.CATEGORY_TABLE, null, values);
}

public long update(Category category) {
    ContentValues values = new ContentValues();
    values.put(DataBaseHelper.NAME_COLUMN, category.getName());

    long result = database.update(DataBaseHelper.CATEGORY_TABLE, values,
            WHERE_ID_EQUALS,
            new String[] { String.valueOf(category.getId()) });
    Log.d("Update Result:", "=" + result);
    return result;

}

public int deleteCategory(Category category) {
    return database.delete(DataBaseHelper.CATEGORY_TABLE,
            WHERE_ID_EQUALS, new String[] { category.getId() + "" });
}

public List<Category> getCategories() {
    List<Category> categories = new ArrayList<Category>();
    Cursor cursor = database.query(DataBaseHelper.CATEGORY_TABLE,
            new String[] { DataBaseHelper.ID_COLUMN,
                    DataBaseHelper.NAME_COLUMN }, null, null, null, null,
            null);

    while (cursor.moveToNext()) {
        Category category = new Category();
        category.setId(cursor.getInt(0));
        category.setName(cursor.getString(1));
        categories.add(category);
    }
    return categories;
}

public void loadCategories() {
    Category movies = new Category("Movies");
    Category music = new Category("Music");
    Category games = new Category("Games");
    Category books = new Category("Books");
    Category food = new Category("Food");

    List<Category> departments = new ArrayList<Category>();
    departments.add(movies);
    departments.add(music);
    departments.add(games);
    departments.add(books);
    departments.add(food);
    for (Category dept : departments) {
        ContentValues values = new ContentValues();
        values.put(DataBaseHelper.NAME_COLUMN, dept.getName());
        database.insert(DataBaseHelper.CATEGORY_TABLE, null, values);
    }
}
}

这是我得到的错误:

    10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime: FATAL EXCEPTION: main
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime: Process: com.dusandimitrijevic.dontforget, PID: 19544
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dusandimitrijevic.dontforget/com.dusandimitrijevic.dontforget.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String)' on a null object reference
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at android.app.ActivityThread.access$900(ActivityThread.java:177)
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145)
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5942)
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String)' on a null object reference
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at helper.CategoryDAO.getCategories(CategoryDAO.java:51)
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at com.dusandimitrijevic.dontforget.MainActivity.onCreate(MainActivity.java:38)
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:6289)
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at android.app.ActivityThread.access$900(ActivityThread.java:177) 
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145) 
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5942) 
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400) 
10-28 18:42:30.868 19544-19544/com.dusandimitrijevic.dontforget E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195) 
4

0 回答 0