Ok I have a big problem here.
I have a service that must accest a listview(in a xml file ofcourse). I tried with findviewbyid,ofcourse it did not work,cause it’s not an activity.
Then I tried to make a static var with that listview in the main activity.Surprise! You can’t make it static cause it uses findviewbyid.
I also tried to make a function in the main activity that returns a listview. The code looks ok,however when the app gets to that function crashes. In the log it apears because of a nullpointer…
Can someone tell me how to acces that listview if I’m in a service? It’s annoying!
In the main activity,at onCreate the code is:
UpdateDisplay();
countdown();
startService(new Intent(this, ss.class));
The source for the listview:
private void UpdateDisplay() //was private and void
{
TextView feedtitle = (TextView) findViewById(R.id.feedtitle);
TextView feedpubdate = (TextView) findViewById(R.id.feedpubdate);
ListView itemlist = (ListView) findViewById(R.id.itemlist);
if (feed == null)
{
feedtitle.setText("No RSS Feed Available");
return;
}
feedtitle.setText(feed.getTitle());
feedpubdate.setText(feed.getPubDate());
ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(this ,android.R.layout.simple_list_item_1,feed.getAllItems());
itemlist.setAdapter(adapter);
itemlist.setOnItemClickListener(this);
itemlist.setSelection(0);
}
This is impossible. Services are not expected to deal with UI elements directly.
You can update your question with WHY do you need a
ListViewin aService. So others could point you in a right direction.