-1

I am creating a app.I've used a tablayout with fragments,extended one of the fragments with ListFragment and wanted to give intent inside that fragment,i used the following...

home_fragment.java

package com.example.mymehnat;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;


public class home_fragment extends ListFragment implements AdapterView.OnItemClickListener {

ListView lst;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.home_fragment, container, false);
    return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    ArrayAdapter adapter = ArrayAdapter.createFromResource(home_fragment.this.getActivity(), R.array.songs, android.R.layout.simple_list_item_1);
    setListAdapter(adapter);
    getListView().setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> adapter, View view,int position, long l) {
    Intent intent = new Intent(home_fragment.this.getActivity(), Lyrics.class);
    intent.putExtra("SongName", lst.getItemAtPosition(position).toString());
    lst.getContext().startActivity(intent);

}

}

home_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background">

<ListView
   android:layout_width="wrap_content"
   android:layout_height="match_parent"
   android:id="@android:id/list">

</ListView>
</LinearLayout>

but whenever I click any item on the list it doesn't work,shows nothing and prompted me an error...

    E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke             virtual method 'java.lang.Object android.widget.ListView.getItemAtPosition(int)' on a null object reference
    at com.example.mymehnat.home_fragment.onItemClick(home_fragment.java:36) and so on

please help me.
your help is highly appreciated.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Phantômaxx Jul 14 '19 at 19:48

1 Answers1

0

You didn't initialize the ListView lst

Try to do like this. This need to be declared under the OnCreateView

lst = v.findViewById(R.id.list)

TheCoderGuy
  • 771
  • 6
  • 24
  • 51