尝试调用虚方法 - NullPointerException

时间:2016-02-17 18:58:17

标签: java android android-fragments nullpointerexception

我在这个网站上尝试了一些关于这个问题实例的解决问题,但是,看起来我的具体是这样,因为它没有显示未初始化对象的例子,而是显示甚至没有调用的方法。

错误日志

java.lang.NullPointerException: Attempt to invoke virtual method 'void rs.ridjis.glosar.rjecnik.RjecnikCursorAdapter.changeCursor(android.database.Cursor)' on a null object reference
    at rs.ridjis.glosar.activities.MainActivity.changeCursor(MainActivity.java:75)
    at rs.ridjis.glosar.activities.MainActivity.onOptionsItemSelected(MainActivity.java:114)
    at android.app.Activity.onMenuItemSelected(Activity.java:2885)
    at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361)
    at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:147)
    at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
    at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
    at android.support.v7.internal.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:73)
    at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:180)
    at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:761)
    at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811)
    at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
    at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958)
    at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948)
    at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:619)
    at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:139)
    at android.view.View.performClick(View.java:4789)
    at android.view.View$PerformClick.run(View.java:19881)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5294)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

MainActivity.java

public class MainActivity extends AppCompatActivity implements Communicator {
public Toolbar toolbar;
private RjecnikCursorAdapter adapter;
private FragmentManager fragmentManager;
private WordsListFragment wordsListFragment;

private RjecnikDB dbRjecnik;
private SQLiteDatabase db;
private Cursor cursor;
private final String QUERY_SELECTALL = "SELECT * FROM " + RjecnikDB.TABLE;

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

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    fragmentManager = getFragmentManager();

    wordsListFragment = new WordsListFragment();

    final FragmentTransaction ft = fragmentManager.beginTransaction();
    ft.add(R.id.fragment_container, wordsListFragment);
    ft.commit();
}

///// Implementation interface methods /////
public void changeCursor() {
    db = dbRjecnik.getWritableDatabase();
    cursor = db.rawQuery(QUERY_SELECTALL, null);
    adapter.changeCursor(cursor);
    db.close();
    Toast.makeText(this, "Lista ažurirana", Toast.LENGTH_SHORT).show();
}

public Cursor initCursor() {
    dbRjecnik = RjecnikDB.getInstance(getApplicationContext());
    db = dbRjecnik.getWritableDatabase();
    cursor = db.rawQuery(QUERY_SELECTALL, null);
    return cursor;
}

public void deleteOnLongClick(int id) {
    db = dbRjecnik.getWritableDatabase();
    db.delete(RjecnikDB.TABLE, RjecnikDB.COLUMN_ID + " = ?", new String[] { Integer.toString(id) } );
    db.close();
}
// END OF IMPLEMENTATION //

@Override
protected void onRestart() {
    super.onRestart();
    changeCursor();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.search_bar) {
        return true;
    } else if (id == R.id.refresh) {
        changeCursor();
    }

    return super.onOptionsItemSelected(item);
}

正如您从错误日志中看到的那样,问题出现在显示changeCursor()的地方,即使它未被调用onOptionItemSelected。 解决这个问题的最佳方法是什么?当我在actionBar初始化之前调用cursor = initCursor()时,它既不工作。

1 个答案:

答案 0 :(得分:0)

在使用之前,您永远不会初始化adapter。这就是为什么它为null并且在你调用

时得到NullPointerException
adapter.changeCursor(cursor);