Android ListFragment NullPointerException错误
我创建了一个列表片段,用来填充RSS数据。唯一的问题是,我收到一个NullPointerException强制关闭我的应用程序。以下是日志:Android ListFragment NullPointerException错误,android,android-listview,nullpointerexception,android-arrayadapter,android-listfragment,Android,Android Listview,Nullpointerexception,Android Arrayadapter,Android Listfragment,我创建了一个列表片段,用来填充RSS数据。唯一的问题是,我收到一个NullPointerException强制关闭我的应用程序。以下是日志: 03-30 15:43:44.584: E/AndroidRuntime(28703): at com.example.app.TestFragment$MyCustomAdapter.getView(TestFragment.java:157) 该错误指向以下代码: listTitle.setText(feed.getList().get(positi
03-30 15:43:44.584: E/AndroidRuntime(28703): at com.example.app.TestFragment$MyCustomAdapter.getView(TestFragment.java:157)
该错误指向以下代码:
listTitle.setText(feed.getList().get(position).getTitle());
listTitle.setText(feed.getList().get(position).getTitle());
从这个班;列表片段
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);
}
}
}
公共最终类TestFragment扩展了ListFragment{
私有静态最终字符串KEY\u CONTENT=“TestFragment:CONTENT”;
公共静态TestFragment newInstance(字符串内容){
TestFragment fragment=新的TestFragment();
返回片段;
}
私有RSSFeed myRssFeed=null;
私有字符串mContent=“???”;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
如果((savedInstanceState!=null)
&&savedInstanceState.containsKey(关键字内容)){
mContent=savedInstanceState.getString(键内容);
}
}
专用RSSFeed feed=null;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
fillData();
返回充气机。充气(R.layout.list_碎片,容器,假);
}
公共数据(){
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(策略);
试一试{
URL rssUrl=新URL(“http://allthingsd.com/feed/");
SAXParserFactory mySAXParserFactory=SAXParserFactory
.newInstance();
SAXParser mySAXParser=mySAXParserFactory.newSAXParser();
XMLReader myXMLReader=myaxParser.getXMLReader();
RSSHandler-myRSSHandler=新的RSSHandler();
setContentHandler(myRSSHandler);
InputSource myInputSource=新的InputSource(rssUrl.openStream());
parse(myInputSource);
myRssFeed=myRSSHandler.getFeed();
}捕获(格式错误){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(ParserConfiguration异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(SAXE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
如果(myRssFeed!=null){
/*
*TextView feedTitle=(TextView)findViewById(R.id.feedTitle);
*TextView FeedDescription=(TextView)
*findViewById(R.id.FeedDescription);文本视图feedPubdate=
*(TextView)findViewById(R.id.feedpubdate);TextView feedLink=
*(TextView)findViewById(R.id.feedlink);
*feedTitle.setText(myRssFeed.getTitle());
*feedDescription.setText(myRssFeed.getDescription());
*feedPubdate.setText(myRssFeed.getPubdate());
*feedLink.setText(myRssFeed.getLink());
*/
MyCustomAdapter=新的MyCustomAdapter(getActivity(),
R.layout.row,myRssFeed.getList());
setListAdapter(适配器);
}
}
@凌驾
SaveInstanceState上的公共无效(束超出状态){
super.onSaveInstanceState(超出状态);
outState.putString(KEY_CONTENT,mContent);
}
@凌驾
public void onListItemClick(列表视图l、视图v、整数位置、长id){
//TODO自动生成的方法存根
/*
*Intent Intent=新的Intent(这个,ShowDetails.class);Bundle=
*新建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);
*星触觉(意向);
*/
}
公共类MyCustomAdapter扩展了ArrayAdapter{
公共MyCustomAdapter(上下文,int textViewResourceId,
(列表){
super(上下文、textViewResourceId、列表);
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
视图行=转换视图;
if(行==null){
LayoutFlater充气机=getActivity().GetLayoutFlater();
行=充气机。充气(R.layout.row,父级,false);
}
TextView listTitle=(TextView)row.findViewById(R.id.textView2);
setText(feed.getList().get(position.getTitle());
TextView listPubdate=(TextView)row.findViewById(R.id.textView1);
listPubdate.setText(myRssFeed.getList().get(position.getPubdate());
如果(位置%2==0){
listTitle.setBackgroundColor(0xff101010);
listPubdate.setBackgroundColor(0xff101010);
}否则{
listTitle.setBackgroundColor(0xff080808);
listPubdate.setBackgroundColor(0xff080808);
}
返回super.getView(position、convertView、parent);
}
}
}
该错误指向以下代码:
listTitle.setText(feed.getList().get(position).getTitle());
listTitle.setText(feed.getList().get(position).getTitle());
查找NullPointerException相对容易,只需检查您引用的每个变量。例如,我看不到您在哪里实例化提要
,您在这里声明它:
private RSSFeed feed = null;
但它从未接收到除
null
以外的值,在某些地方您需要像feed=new RSSFeed()这样的代码代码>。。。(您是否打算使用myRSSFeed
?)检查您的row.xml id。这可能会导致空指针 你检查过那一行的每个变量了吗?首先,row.xml
是否有id为@+id/textView2
的TextView?我在这里实例化提要:myRssFeed=myRSSHandler.getFeed();这是fillData()语句中的try-catch块,它是myRssFeed
而不是feed
,您看到区别了吗?