0

I have an activity with fragment which contains ListView, the app is building and deploying without problem but i get the APP HAS STOPPED working message when i populate the listView.

If i comment out the listview population with the adapter, the app runs without any problem.

This is the code:

public class MainActivityFragment extends Fragment {
    private ArrayAdapter<String> weather_adapter;

    public MainActivityFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        String[] data = {
                "item1",
                "item2"
};
        List<String> list = new ArrayList<String>(Arrays.asList(data));

        weather_adapter = new ArrayAdapter<String>(
                getActivity(),
                R.layout.l_list_item,
                R.id.list_view,
                list );

        ListView welist =
                (ListView)rootView.findViewById(R.id.list_view);
        wlist.setAdapter(adapter);

        return rootView;
    }
}

Do you have any idea how to detect what is wrong since stacktrace doesnt throw any errors or warnings?

Tim
  • 177
  • 2
  • 13
  • 2
    The ID of the TextView in R.layout.l_list_item is R.id.list_view? I don't think so. Check your new ArrayAdapter... – Eugen Pechanec Oct 10 '16 at 20:44
  • 2
    You are getting a runtime exception. Doesn't matter if the app built successfully. There *should* be a logcat. http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – OneCricketeer Oct 10 '16 at 20:44
  • @EugenPechanec that is the solution of this problem however i am still interested in a way to get theese errors on compile time. Logcat throws no errors whatsoever. – Tim Oct 10 '16 at 20:56

1 Answers1

1

In your Array adapter initialization ,

 weather_adapter = new ArrayAdapter<String>(
            getActivity(),
            R.layout.l_list_item,
            R.id.list_view,
            list );

Third parameter should be the textViews id (where the String list to be placed)

Change it to,

weather_adapter = new ArrayAdapter<String>(
            getActivity(),
            R.layout.l_list_item,
            R.id.textView,
            list );

You can find this id in your R.layout.l_list_item Layout.

If it is not solving. Provide your xml files and log.

Ajith Pandian
  • 1,332
  • 13
  • 21