我在android studio中使用rss feed创建了一个Android应用程序,因此我尝试将标题和说明从http://feeds.bbci.co.uk/news/rss.xml放到使用自定义适配器的两个文本视图中。 rss Feed位于选项卡中。每次我尝试运行我的程序时,我都会遇到异常,问题出现在这个类中:
public class LAdapter extends ArrayAdapter<Info> {
public LAdapter(Context context, ArrayList<Info> nfos) {super(context, 0,nfos);}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
Info info = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.basic_list_item, parent, false);
}
// Lookup view for data population
TextView title = (TextView) convertView.findViewById(R.id.Tit);
TextView description = (TextView) convertView.findViewById(R.id.Desc);
title.setText(info.Title);
description.setText(info.Description);
// Return the completed view to render on screen
return convertView;
}
}
Partial Tab_1.class:
public class Tab_1 extends Fragment {
ListView mList;
ArrayList<Info> arrayOfInfo;
LAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.tab_1, container, false);
LAdapter adapter = new LAdapter(getActivity(), arrayOfInfo);
#Line 44-> mList = (ListView) v.findViewById(R.id.list);
new GetRssFeed().execute("http://feeds.bbci.co.uk/news/rss.xml");
mList.setAdapter(adapter);
return v;
}
例外:
Process: com.example.pauly.myapplication, PID: 3896
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
at android.widget.ListView.setAdapter(ListView.java:487)
at com.example.pauly.myapplication.Tabs.Tab_1.onCreateView(Tab_1.java:43)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
at
android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1072)
at android.support.v4.view.ViewPager.populate(ViewPager.java:918)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1440)
...
答案 0 :(得分:0)
LAdapter adapter = new LAdapter(getActivity(), arrayOfInfo);
arrayOfInfo为null(它甚至没有初始化),因为你在arrayOfInfo之后调用了你的asynctask。使用从DoInBackground方法接收的数据,您可以使用asynctask中的OnPostExecute方法中的数据设置arrayOfInfo,并且在使用数据设置此数组之后,您也可以调用OnPostExecute
LAdapter adapter = new LAdapter(getActivity(), arrayOfInfo);
mList.setAdapter(adapter);
OnPostExecute基本上是处理你的UI。