gpt4 book ai didi

java - 通过长按 ListView 项目,Android onCreateContextMenu 致命异常

转载 作者:行者123 更新时间:2023-11-29 05:34:13 29 4
gpt4 key购买 nike

我尝试为我的 ListView 创建上下文菜单,但是当我“长按”列表项时,应用程序关闭并显示此跟踪:

FATAL EXCEPTION: main
java.lang.NullPointerException
at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onCreateActionMode(PhoneWindow.java:2445)
at com.android.internal.app.ActionBarImpl$ActionModeImpl.dispatchOnCreate(ActionBarImpl.java:708)
at com.android.internal.app.ActionBarImpl.startActionMode(ActionBarImpl.java:392)
at android.app.Activity.onWindowStartingActionMode(Activity.java:4380)
at com.android.internal.policy.impl.PhoneWindow$DecorView.startActionMode(PhoneWindow.java:2151)
at com.android.internal.policy.impl.PhoneWindow$DecorView.startActionModeForChild(PhoneWindow.java:2138)
at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:573)
at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:573)
at android.view.ViewGroup.startActionModeForChild(ViewGroup.java:573)
at android.view.View.startActionMode(View.java:3558)
at android.widget.AbsListView.performLongPress(AbsListView.java:2570)
at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:2530)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4340)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

我浏览了很多示例和教程,但找不到决定。安卓 API 14。

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

linkButton = (Button) findViewById(R.id.link_button);
debugTxt = (TextView) findViewById(R.id.debugText);
lv = (ListView) this.findViewById(R.id.filelist);

registerForContextMenu(lv);

linkButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onClickLink();
}
});

mDbxAcctMgr = DbxAccountManager.getInstance(getApplicationContext(), appKey, appSecret);
}

@Override
protected void onResume() {
super.onResume();
if (mDbxAcctMgr.hasLinkedAccount()) {
showLinkedView();
doDropboxTest();
} else {
showUnlinkedView();
}
}

private void onClickLink() {
mDbxAcctMgr.startLink((Activity)this, REQUEST_LINK_TO_DBX);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_LINK_TO_DBX) {
if (resultCode == Activity.RESULT_OK) {
doDropboxTest();
} else {
debugTxt.setText("Link to Dropbox failed or was cancelled.");
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}

private void doDropboxTest() {

try {
// Create DbxFileSystem for synchronized file access.
DbxFileSystem dbxFs = DbxFileSystem.forAccount(mDbxAcctMgr.getLinkedAccount());
items = new ArrayList<String>();

// Print the contents of the root folder. This will block until we can
// sync metadata the first time.
List<DbxFileInfo> infos = dbxFs.listFolder(DbxPath.ROOT);
debugTxt.setText("Contents of app folder:\n");

for (DbxFileInfo info : infos) {
info.path.toString();
if (info.isFolder) {
items.add(info.path.getName() + "/");
} else {
items.add(info.path.getName());
}
}

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);

lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
//lv.setLongClickable(true);
lv.setAdapter(adapter);

} catch (IOException e) {
debugTxt.setText("Dropbox test failed: " + e);
}


}

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
android:id="@+id/edit"
android:title="@string/edit" />
<item
android:id="@+id/remove"
android:title="@string/remove" />
<item
android:id="@+id/change"
android:title="@string/change" />

</menu>

谢谢帮助!

最佳答案

当您使用 registerForContextMenu() 为上下文菜单注册 View 时,Activity 将设置为接收 onCreateContextMenu() 调用。删除 lv.setOnCreateContextMenuListener() 调用,并将 onCreateContextMenu() 方法移动到 Activity。

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

linkButton = (Button) findViewById(R.id.link_button);
debugTxt = (TextView) findViewById(R.id.debugText);
lv = (ListView) this.findViewById(R.id.filelist);

registerForContextMenu(lv);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
lv.setAdapter(adapter);

linkButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
onClickLink();
}
});

mDbxAcctMgr = DbxAccountManager.getInstance(getApplicationContext(), appKey, appSecret);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
{ //here u set u rute
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.login, menu);
}

此外,当您为上下文菜单注册 lv 时,您无需调用 lv.setLongClickable()

关于java - 通过长按 ListView 项目,Android onCreateContextMenu 致命异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20065903/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com