startSupportActionMode() 抛出 NullPointerException
startSupportActionMode() throws NullPointerException
我在 AppCompatActivity
中调用 startSupportActionMode()
。
此 activity 是通过在 Android Studio 中单击 "Basic Activity" 创建的。
它使用 AppBarLayout
和 ToolBar
创建 ActionBar
。
这是我的Activity:
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
?
发现我的错误,原因是下面一行
listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
当我将其更改为
时它正在工作
listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
但我仍然对它为什么抛出 NullPointerException
以及为什么 CHOICE_MODE_MULTIPLE_MODAL
调用 onCreateActionMode()
.
感到困惑
我在 AppCompatActivity
中调用 startSupportActionMode()
。
此 activity 是通过在 Android Studio 中单击 "Basic Activity" 创建的。
它使用 AppBarLayout
和 ToolBar
创建 ActionBar
。
这是我的Activity:
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
?
发现我的错误,原因是下面一行
listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE_MODAL);
当我将其更改为
时它正在工作listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
但我仍然对它为什么抛出 NullPointerException
以及为什么 CHOICE_MODE_MULTIPLE_MODAL
调用 onCreateActionMode()
.