0

I am getting this error in the oncreate method. It seems to be related to the List i created beforehand. Could you give any suggestions what the activity could be or how i can find that out?

public class MainActivity extends AppCompatActivity {

    private List<ListItem> items;
    private ArrayAdapter<String> adapter;
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


        items.add(new ListItem("test", "https://google.com"));  

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

        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);

        listView.setAdapter(adapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}

public class ListItem {

    private long id;
    private String url;
    private String title;

    public ListItem(String title, String url) {
        this.title = title;
        this.url = url;
    }

    public String getUrl() {
        return url;
    }

    @Override
    public String toString() {
        return title;
    }
}

If i change the private list to String instead of ListItem it does work, any suggestions on why or how?

1
  • items is never initialized here. This will give you NPE. Commented Apr 2, 2018 at 6:17

2 Answers 2

0

You are telling the ArrayAdapter that you will be using a String but you are passing ListItem in parameters.

Change

adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);

to

adapter = new ArrayAdapter<ListItem>(getActivity(), android.R.layout.simple_list_item_1, items);

and

private ArrayAdapter<String> adapter;

to

private ArrayAdapter<ListItem> adapter;
Sign up to request clarification or add additional context in comments.

4 Comments

excuse me a small typo i did use adapter = new ArrayAdapter<String>(this, ...) before. I have changed it according to you but still get an error: required java.lang.string and found studentportal.ListItem. I tried putting private ArrayAdapter<String> adapter; to private ArrayAdapter<ListItem> adapter; however i get a NullPointerException then
At every place you should have ArrayAdapter<ListItem>. Don't use String anywhere. Also you should have List<ListItem>
I did that exactly but then i get "Unable to start activity ComponentInfo" with a "java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add"
Change private List<ListItem> items = new ArrayList<ListItem>();. You need to initialize it before adding item to the list.
0

Just add following line in your code.

item = new ArrayList<ListItem>;

Just above the following line.

items.add(new ListItem("test", "https://google.com"));  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.