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?
itemsis never initialized here. This will give you NPE.