gpt4 book ai didi

android - 禁用 ListView 的项目 - 我的代码出现异常

转载 作者:行者123 更新时间:2023-11-29 19:56:53 27 4
gpt4 key购买 nike

<分区>

我在这里阅读了几篇关于此的文章,但我不明白我错在哪里以及为什么它在我的简单案例中不起作用。

我的目标是在 myCondition 为 false 时禁用(我也希望它们显示为浅灰色)这 2 个项目。

//__________________________
/

final Context myContext = this;
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(myContext);
final ListView listView = new ListView(myContext);
List myListOptions = Arrays.asList(
"item 0", //position=0
"item 1" //position=1
) ;
final ArrayAdapter arrayAdapter = new ArrayAdapter(myContext, android.R.layout.simple_list_item_1, myListOptions );
listView.setAdapter(arrayAdapter);

//*****HERE THE PROBLEM IS!!!*********
if (myCondition == false) {
listView.getChildAt(0).setEnabled(false);
listView.getChildAt(1).setEnabled(false);
}

alertDialogBuilder.setView(listView);
final AlertDialog alertDialog = alertDialogBuilder.create();
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//__________________________
//__________________________
//longclick su Item 0
if (position == 0){
//Do something
}

//__________________________
//__________________________
//longclick su CopiaTesto
if (position == 1) {
//Do something
}
//__________________________
//__________________________

alertDialog.dismiss();

return true;
}
});
alertDialog.show();
return true;
//__________________________

我在行 if (myCondition==false) 上得到一个异常:

04-17 07:06:27.471  25198-25198/com.test E/﹕ Classe 'HeaderFragment'  ----  'onClick Opzioni'  ----  Exception: null        ----  Nome Thread (che invoca il Log)='main'
java.lang.NullPointerException
at com.test.HeaderFragment$2$3.onLongClick(HeaderFragment.java:462)
at android.view.View.performLongClick(View.java:4142)
at android.view.View$CheckForLongPress.run(View.java:17031)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5118)
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:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
at dalvik.system.NativeStart.main(Native Method)

我已经知道什么是Null Pointer Exception,但我不明白为什么我在这种情况下会有它,可能是因为我不熟悉arrayAdapter

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