startSupportActionMode()抛出NullPointerException

时间:2017-11-24 18:19:22

标签: android nullpointerexception android-toolbar appcompatactivity contextual-action-bar

我在startSupportActionMode()内拨打AppCompatActivity

此活动是通过点击"基本活动"创建的。在Android Studio中。

它使用AppBarLyoutToolBar来创建ActionBar

这是我的活动:

 import android.support.v7.view.ActionMode;
 public class ListActivity extends AppCompatActivity implements AdapterView.OnItemClickListener,AdapterView.OnItemLongClickListener, ActionMode.Callback{

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(view -> {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    });

    Button buttonLoadResult = findViewById(R.id.buttonLoadResult);
    buttonLoadResult.setOnClickListener(v -> {
        Intent intent = new Intent(ListActivity.this, MainActivity.class);
        startActivity(intent);
    });

    emptyStateLayout = findViewById(R.id.layoutEmptyState);
    listAdapter = new ListAdapter(this, getFileList());
    listView = findViewById(R.id.listViewResultList);
    listView.setOnItemClickListener(this);
    listView.setOnItemLongClickListener(this);
    listView.setAdapter(listAdapter);
    listView.setLongClickable(true);
    listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);

    setAppropriateViews();
}

//Some codes...

 @Override
 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
    listView.setItemChecked(position,true);
    if (activeMode == null) {
        activeMode = this.startSupportActionMode(this);
        return true;
    }
    return false;
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    getMenuInflater().inflate(R.menu.menu_action_mode, menu);
    return true;
}

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
    return false;
}

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    return false;
}

@Override
public void onDestroyActionMode(ActionMode mode) {
    activeMode = null;
}
}

提供以下日志:

java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.view.ActionMode$Callback.onCreateActionMode(android.view.ActionMode, android.view.Menu)' on a null object reference
    at com.android.internal.policy.DecorView$ActionModeCallback2Wrapper.onCreateActionMode(DecorView.java:2286)
    at android.support.v7.view.SupportActionModeWrapper$CallbackWrapper.onCreateActionMode(SupportActionModeWrapper.java:157)
    at android.support.v7.app.AppCompatDelegateImplV9$ActionModeCallbackWrapperV9.onCreateActionMode(AppCompatDelegateImplV9.java:1793)
    at android.support.v7.app.AppCompatDelegateImplV9.startSupportActionModeFromWindow(AppCompatDelegateImplV9.java:824)
    at android.support.v7.app.AppCompatDelegateImplV9.startSupportActionMode(AppCompatDelegateImplV9.java:707)
    at android.support.v7.app.AppCompatDelegateImplV14$AppCompatWindowCallbackV14.startAsSupportActionMode(AppCompatDelegateImplV14.java:292)
    at android.support.v7.app.AppCompatDelegateImplV23$AppCompatWindowCallbackV23.onWindowStartingActionMode(AppCompatDelegateImplV23.java:66)
    at android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode(WindowCallbackWrapper.java:160)
    at com.android.internal.policy.DecorView.startActionMode(DecorView.java:835)
    at com.android.internal.policy.DecorView.startActionModeForChild(DecorView.java:816)
    at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:828)
    at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:828)
    at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:828)
    at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:828)
    at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:828)
    at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:828)
    at android.view.View.startActionMode(View.java:5828)
    at android.view.View.startActionMode(View.java:5812)
    at android.widget.AbsListView.performLongPress(AbsListView.java:3222)
    at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:3148)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6119)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

为什么我收到NullPointerException

1 个答案:

答案 0 :(得分:0)

我发现了我的错误,这是由于以下一行

listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);

当我将其更改为

时,它正在工作
listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);

但我仍然对为什么抛出NullPointerException以及为什么CHOICE_MODE_MULTIPLE_MODAL调用onCreateActionMode()感到困惑。