[英]ListFragment NullPointerException error
I've created a ListFragment that is meant to be filled with RSS data. 我创建了一个ListFragment ,它打算用RSS数据填充。 The only problem is that I'm receiving a NullPointerException that force closes my app.
唯一的问题是我收到强制关闭我的应用程序的NullPointerException 。 Here is the LogCat:
这是LogCat:
03-30 15:43:44.584: E/AndroidRuntime(28703): at com.example.app.TestFragment$MyCustomAdapter.getView(TestFragment.java:157)
That error points to this code: 该错误指向以下代码:
listTitle.setText(feed.getList().get(position).getTitle());
From this class; 从这个班上; ListFragment.
ListFragment。
public final class TestFragment extends ListFragment {
private static final String KEY_CONTENT = "TestFragment:Content";
public static TestFragment newInstance(String content) {
TestFragment fragment = new TestFragment();
return fragment;
}
private RSSFeed myRssFeed = null;
private String mContent = "???";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ((savedInstanceState != null)
&& savedInstanceState.containsKey(KEY_CONTENT)) {
mContent = savedInstanceState.getString(KEY_CONTENT);
}
}
private RSSFeed feed = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
fillData();
return inflater.inflate(R.layout.list_fragment, container, false);
}
public void fillData() {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
URL rssUrl = new URL("http://allthingsd.com/feed/");
SAXParserFactory mySAXParserFactory = SAXParserFactory
.newInstance();
SAXParser mySAXParser = mySAXParserFactory.newSAXParser();
XMLReader myXMLReader = mySAXParser.getXMLReader();
RSSHandler myRSSHandler = new RSSHandler();
myXMLReader.setContentHandler(myRSSHandler);
InputSource myInputSource = new InputSource(rssUrl.openStream());
myXMLReader.parse(myInputSource);
myRssFeed = myRSSHandler.getFeed();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (myRssFeed != null) {
/*
* TextView feedTitle = (TextView) findViewById(R.id.feedtitle);
* TextView feedDescribtion = (TextView)
* findViewById(R.id.feeddescribtion); TextView feedPubdate =
* (TextView) findViewById(R.id.feedpubdate); TextView feedLink =
* (TextView) findViewById(R.id.feedlink);
* feedTitle.setText(myRssFeed.getTitle());
* feedDescribtion.setText(myRssFeed.getDescription());
* feedPubdate.setText(myRssFeed.getPubdate());
* feedLink.setText(myRssFeed.getLink());
*/
MyCustomAdapter adapter = new MyCustomAdapter(getActivity(),
R.layout.row, myRssFeed.getList());
setListAdapter(adapter);
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(KEY_CONTENT, mContent);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
/*
* Intent intent = new Intent(this,ShowDetails.class); Bundle bundle =
* new Bundle(); bundle.putString("keyTitle",
* myRssFeed.getItem(position).getTitle());
* bundle.putString("keyDescription",
* myRssFeed.getItem(position).getDescription());
* bundle.putString("keyLink", myRssFeed.getItem(position).getLink());
* bundle.putString("keyPubdate",
* myRssFeed.getItem(position).getPubdate()); intent.putExtras(bundle);
* startActivity(intent);
*/
}
public class MyCustomAdapter extends ArrayAdapter<RSSItem> {
public MyCustomAdapter(Context context, int textViewResourceId,
List<RSSItem> list) {
super(context, textViewResourceId, list);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
if (row == null) {
LayoutInflater inflater = getActivity().getLayoutInflater();
row = inflater.inflate(R.layout.row, parent, false);
}
TextView listTitle = (TextView) row.findViewById(R.id.textView2);
listTitle.setText(feed.getList().get(position).getTitle());
TextView listPubdate = (TextView) row.findViewById(R.id.textView1);
listPubdate.setText(myRssFeed.getList().get(position).getPubdate());
if (position % 2 == 0) {
listTitle.setBackgroundColor(0xff101010);
listPubdate.setBackgroundColor(0xff101010);
} else {
listTitle.setBackgroundColor(0xff080808);
listPubdate.setBackgroundColor(0xff080808);
}
return super.getView(position, convertView, parent);
}
}
}
That error points to this code:
该错误指向以下代码:
listTitle.setText(feed.getList().get(position).getTitle());
Finding a NullPointerException is relatively easy, simply check each variable that you are referencing. 查找NullPointerException相对容易,只需检查您引用的每个变量即可。 For instance, I don't see where you instantiate
feed
, you declare it here: 例如,我看不到实例化
feed
,而是在此处声明它:
private RSSFeed feed = null;
But it never receives a value other than null
, somewhere you need the code like feed = new RSSFeed();
但是它永远不会收到
null
以外的null
,在某些地方您需要像feed = new RSSFeed();
这样的代码feed = new RSSFeed();
... (Did you mean to use myRSSFeed
?) ...(您是要使用
myRSSFeed
吗?)
检查您的row.xml id,这可能导致您的空指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.