Attempt to invoke virtual method 'void android.widget.TextView.setText (java.lang.CharSequence)' on a null object reference

1

The error I got when doing Debug from my app was:

Attempt to invoke virtual method 'void android.widget.TextView.setText (java.lang.CharSequence)' on a null object reference

I have a custom adapter called MyAdapter where I try to load a design that supposedly should be inserted in ListView .

ListView

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragments.NewsFragment">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</FrameLayout>

Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:id="@+id/list_item"
    android:layout_height="80dp"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageabdcf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_margin="25dp"
        tools:srcCompat="@android:drawable/btn_star_big_on" />

    <TextView
        android:id="@+id/textabcdf"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_weight="1"
        android:text="TextView" />
</LinearLayout>

NewsFragment.java

public class NewsFragment extends Fragment {

    private ListView listView;
    private List<String> names;
    public NewsFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.fragment_news, container, false);

        listView = (ListView) v.findViewById(R.id.listView);

        names = new ArrayList<String>();
        names.add("Fernando");
        names.add("Roberto");
        names.add("Torres");
        names.add("Urban");

        ArrayAdapter<String> adapter = new ArrayAdapter<>(v.getContext(), android.R.layout.simple_list_item_1, names);

        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getView().getContext(), "Clicked: " + names.get(position), Toast.LENGTH_LONG).show();
            }
        });

        MyAdapter myAdapter = new MyAdapter(v.getContext(), R.layout.card_view_news, names);
        listView.setAdapter(myAdapter);

        // Inflate the layout for this fragment
        return v;
    }

}

MyAdapter.java

public class MyAdapter extends BaseAdapter {

    private Context context;
    private int layout;
    private List<String> names;

    public MyAdapter(Context context, int layout, List<String> names) {
        this.context = context;
        this.layout = layout;
        this.names = names;
    }

    @Override
    public int getCount() {
        return names.size();
    }

    @Override
    public Object getItem(int position) {
        return this.names.get(position);
    }

    @Override
    public long getItemId(int id) {
        return id;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {

        View v = convertView;
        LayoutInflater layoutInflater = LayoutInflater.from(this.context);
        v = layoutInflater.inflate(R.layout.fragment_news, null);
        String currentName = names.get(position);    

        TextView textView = (TextView) v.findViewById(R.id.textabcdf);
        textView.setText(currentName);

        return v;
    }
}

What I'm waiting for is something like:

What can be happening inside the code that is returning this error to me and that surely it is an error related to the context?

    
asked by Fernando Urban 17.12.2018 в 05:07
source

2 answers

0

Hello, apparently you are inflating the view of the item wrong.

// En esta linea del getView del adapter estas inflando el layout del fragment 
v = layoutInflater.inflate(R.layout.fragment_news, null);

That layout does not contain the textView with id=textabcdf that's why you end up giving NullPointerException

Simply change fragment_news to the layout name of list_item

    
answered by 17.12.2018 в 07:54
0

The problem is that the layout you inflate fragment_news.xml :

    v = layoutInflater.inflate(R.layout.fragment_news, null);

does not actually contain the TextView with id textabcdf

    TextView textView = (TextView) v.findViewById(R.id.textabcdf);

Therefore when trying to call the setText () method it is being done in an instance that has a null value:

    textView.setText(currentName);

Ensure that the file fragment_news.xml contains the TextView textabcdf

    
answered by 18.12.2018 в 00:44