我正在使用swipe tab
菜单生成应用程序的视图,在开始时我除了生成选项卡的布局之外,我还在运行async task
Rss 阅读器。目前我有三个标签,Rss 提要在第一个标签上。问题是当我转到第三个选项卡并返回第一个选项卡时,提要消失了。我认为这是因为async task
检索 Rss 提要仅在主要活动中运行,但是当我尝试在呈现第一个选项卡时调用此任务时,我不断获取null pointer exception
(text view
如果没有网络连接)或 on listview
,我不知道为什么这没有指向正确的布局,有什么想法吗?
这是异步任务的代码:
public class GetRSSDataTask extends AsyncTask<String, Void, List<RssItem>> {
private Activity ownerActivity;
private Exception exceptionToBeThrown;
@Override
protected List<RssItem> doInBackground(String... urls) {
// Debug the task thread name
Log.d("RssReader", Thread.currentThread().getName());
try {
// Create RSS reader
RssReader rssReader = new RssReader(urls[0]);
// Parse RSS, get items
return rssReader.getItems();
} catch (Exception e) {
Log.e("RssReader", e.getMessage());
exceptionToBeThrown = e;
}
return null;
}
@Override
protected void onPostExecute(List<RssItem> result) {
if (exceptionToBeThrown != null) {
TextView error = (TextView) findViewById(R.id.errorMessage);
error.setText("No internet connection"); //Null pointer expection
} else {
// Get a ListView from main view
ListView listItems= (ListView) findViewById(R.id.listMainView); //Null pointer exception
// Create a list adapter
ArrayAdapter<RssItem> adapter = new ArrayAdapter<RssItem>(local, android.R.layout.simple_list_item_1, result);
// Set list adapter for the ListView
listItems.setAdapter(adapter);
// Set list view item click listener
listItems.setOnItemClickListener(new ListListener(result, local));
}
}
}
这是我的 rss 提要选项卡的代码:
public class RssFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
new MainActivity();
MainActivity ma = new MainActivity();
GetRSSDataTask task = ma.new GetRSSDataTask();
task.execute("newsrss.bbc.co.uk/rss/newsonline_uk_edition/world/rss.xml");
View rootView = inflater.inflate(R.layout.fragment_rss_news, container, false);
return rootView;
}
}
在主要活动中创建:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set reference to this activity
local = this;
GetRSSDataTask task = new GetRSSDataTask();
// Start download RSS task
task.execute("newsrss.bbc.co.uk/rss/newsonline_uk_edition/world/rss.xml");
// Debug the thread name
Log.d("RssReader", Thread.currentThread().getName());
// Initilization
viewPager = (ViewPager) findViewById(R.id.pager);
actionBar = getActionBar();
mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
//actionBar.setHomeButtonEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// Adding Tabs
for (String tab_name : tabs) {
actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
}
/**
* on swiping the viewpager make respective tab selected
* */
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// on changing the page
// make respected tab selected
actionBar.setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
}