我使用swipe tab
菜单生成应用视图,在启动时我除了生成标签的布局外,我还在运行{{1} },这是Rss阅读器。目前我有三个标签,Rss Feed在第一个标签上。问题是,当我转到第三个标签并返回第一个标签时,Feed会消失。我认为这是因为检索Rss提要的async task
仅在主活动中运行,但是当我在呈现第一个选项卡时尝试调用此任务时,我继续async task
获取null pointer exception
1}}(如果没有网络连接)或text view
,我不确定为什么没有指向正确的布局,任何想法?
这是异步任务的代码:
listview
}
以下是我的RSS Feed标签的代码:
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));
}
}
在主要活动中创建:
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;
}
}
答案 0 :(得分:0)
说明:尝试在GetRSSDataTask
内编写RssFragment
课程,然后在RssFragment
上的onCreateView
拨打该课程。该错误是因为您创建main activity
的实例以在GetRSSDataTask
中调用RssFragment
。
<强>解决方案:强> RssFragment.java:
public class RssFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
GetRSSDataTask task = 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;
}
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));
}
}
}
MainActivity.java onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set reference to this activity
local = this;
// 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) {
}
});
}