0

I have this code...

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final RelationShipAdapter adapter = null;
        lvRelationShipAllUser.setOnItemClickListener(new AdapterView.OnItemClickListener(){

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int i, long l) {
                int test = i;
                connectOrDisconnectUser(test);
            }
        });

        lvRelationShipAllUser = (ListView) getView().findViewById(R.id.lvRelationShip);
        lvRelationShipAllUser.setAdapter(adapter);
        //context = BlankFragment.this;
        return inflater.inflate(R.layout.relationship, container, false);
    }

The error says:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setOnItemClickListener(android.widget.AdapterView$OnItemClickListener)' on a null object reference

On this line:

lvRelationShipAllUser.setOnItemClickListener(new AdapterView.OnItemClickListener(){

Whats the issue....?

When it's in this own class (No Fragments) there is no issue.

  • Please show how you have initialized lvRelationShipAllUser. – Nabin Dec 06 '17 at 04:04
  • final RelationShipAdapter adapter = null; , where do you create the adapter? – diegoveloper Dec 06 '17 at 04:04
  • You're trying to call `setOnItemClickListener()` on `lvRelationShipAllUser` before you even find it, and you're not doing that correctly for a `Fragment`. Look at this answer: https://stackoverflow.com/a/6496013. – Mike M. Dec 06 '17 at 04:05
  • private ListView lvRelationShipAllUser; –  Dec 06 '17 at 04:07
  • you need initialize the adapter: final RelationShipAdapter adapter = new RelationShipAdapter .. ; – diegoveloper Dec 06 '17 at 04:09
  • @MikeM. Can you help me out, be more specific. What should I do? –  Dec 06 '17 at 04:11
  • Follow the example in that answer: `View v = inflater.inflate(R.layout.relationship, ...);`, `lvRelationShipAllUser = (ListView) v.findViewById(R.id.lvRelationShip);`, then the other lines, then change the return to `return v;`. – Mike M. Dec 06 '17 at 04:14
  • can you answer with the exact code I would use? I am a noob. –  Dec 06 '17 at 04:16
  • https://pastebin.com/M9WDsq6N – Mike M. Dec 06 '17 at 04:20

0 Answers0