0

I've spent hours attempting to debug this code. Basically I have MainActivity with the default code you get after starting a new project. And this is my MainActivityFragment:

package app.xxxx.github.io.sunshine;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.Arrays;
import java.util.List;
public class MainActivityFragment extends Fragment {

    public MainActivityFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        String[] fakeData = new String[]{
                "Today - Sunny - 88/63",
                "Tomorrow - Foggy - 70/46",
                "Weds - Cloudy - 72/63",
                "Thurs - Rainy - 64/51",
                "Fri - Foggy - 70/46",
                "Sat - Sunny - 76/68"
        };
        List<String> weatherData = Arrays.asList(fakeData);
        ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast,
                    R.id.listview_forecast, weatherData);
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        ListView listview = (ListView) rootView.findViewById(R.id.listview_forecast);
        listview.setAdapter(myAdapter);
        return rootView;}}

As you can see, I'm just attempting to show some dummy strings to the screen.

Here is FragmentMain.xml:

<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="app.xxxx.github.io.sunshine.MainActivityFragment"
    tools:showIn="@layout/activity_main">

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

</FrameLayout>

and list_item_forecast.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:id="@+id/list_item_forecast_textview" />

Here is what the Stack Trace says:

FATAL EXCEPTION: main Process: app.xxxx.github.io.sunshine, PID: 4652 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:399) at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)

Sorry if it got a little bit ugly but had to give enough context for the problem. Thanks.

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
Meta Xenology
  • 142
  • 2
  • 9
  • You're passing the wrong `R.id` in the `ArrayAdapter` constructor call. It needs to be the ID of the `TextView` in the list item layout - `R.id.list_item_forecast_textview` - or just remove that argument, if the layout is just the `TextView`. – Mike M. Jun 14 '16 at 03:02
  • The **id** being `R.id.list_item_forecast_textview`. – Daniel Jun 14 '16 at 03:07

4 Answers4

0

You should provide the id of the list_item_TextView but not the ListView itself. So change your code as

 ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast,
                R.id.list_item_forecast_textview, weatherData);
Sai Raman Kilambi
  • 878
  • 2
  • 12
  • 29
0

Are you giving wrong id of textview in adapter

 ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast,
                    R.id.listview_forecast, weatherData);

so replace this line with

 ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast,
                    R.id.list_item_forecast_textview, weatherData);
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
Vijay Rajput
  • 1,091
  • 1
  • 13
  • 18
0

Fix your TextView's id into the declaration of ArrayAdapter:

 ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast,
                        R.id.list_item_forecast_textview, weatherData);

Or, use the built-in TextView as following:

 ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast,
                        android.R.id.text1, weatherData);
Khairul Alam Licon
  • 420
  • 10
  • 19
0

You are using this constructor to create adapter instance.

    /**
     * Constructor
     *
     * @param context The current context.
     * @param resource The resource ID for a layout file containing a layout to use when
     *                 instantiating views.
     * @param textViewResourceId The id of the TextView within the layout resource to be populated
     * @param objects The objects to represent in the ListView.
     */
    public ArrayAdapter(Context context, @LayoutRes int resource, @IdRes int textViewResourceId,
            @NonNull T[] objects) {
        this(context, resource, textViewResourceId, Arrays.asList(objects));
    }

It uses

@param textViewResourceId The id of the TextView within the layout resource to be populated

Because you are using custom layout, so you have to pass right textView id to populate data

simply change

ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast,
                R.id.listview_forecast, weatherData);

to

ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast,
                R.id.list_item_forecast_textview, weatherData);
Alex Hong
  • 281
  • 1
  • 11