1

I have read several posts here about that, but I didn't understand what I'm wrong and why it doesn't work in my simple case.

My target is to disable (and also I would like to see them in light gray) the 2 items when myCondition is false.

//__________________________
/

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;
//__________________________

I get an exception on the line 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)

I already knew what is a Null Pointer Exception, but I didn't understand why I have it in this case, probably because I'm not familiar with arrayAdapter

Fausto70
  • 541
  • 5
  • 20
  • **1.** It says `HeaderFragment.java:462`. What's on line 462 of `HeaderFragment.java`? **2.** What is the data type of `myCondition`? – Pang Apr 17 '16 at 05:55
  • HeaderFragment is the name of the class where this code is; myCondition is a whatever boolean var, that can be true or false depending from other things of my app. Finally " What is a Null Pointer Exception, and how do I fix it" is too generic, I already knew what is a Null Pointer Exception, but I didn't understand why I have it in this case, probably because I'm not familiar with arrayAdapter – Fausto70 Apr 17 '16 at 12:17

1 Answers1

2

The reason you get NullPointerException is that when you call the following line:

listView.getChildAt(0).setEnabled(false);

you ask the listView to give you the View at position 0 from the listview. However the view has not yet been inflated so there is no View to return.

If you want to have custom behavior for your ListView, I suggest trying a custom adapter instead of ArrayAdapter. Custom adapter can be extended from ArrayAdapter and you have more freedom in how you want to represent your views. You can find a comprehensive tutorial here.

Pooya
  • 6,083
  • 3
  • 23
  • 43