1

我创建了一个ListFragment这意味着要填充RSS数据。唯一的问题是,我收到一个NullPointerException强制关闭我的应用程序。这里是logcat的:ListFragment 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(position).getTitle()); 

从这节课; 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); 

    } 
} 

    } 
+0

您是否检查过该行中的每个变量?首先'row.xml'有一个ID为'@ + id/textView2'的TextView? – Sam

回答

2

该错误指向此代码:

listTitle.setText(feed.getList().get(position).getTitle()); 

找到一个NullPointerException异常是比较容易的,只需检查所引用的每个变量。举例来说,我没有看到你实例feed,你在这里声明它:

private RSSFeed feed = null; 

但一直没有收到比null以外的值,某处你需要像feed = new RSSFeed();代码...(你的意思是使用myRSSFeed?)

+0

我在这里实例化feed:myRssFeed = myRSSHandler.getFeed();这是在fillData()语句 – AndroidDev

+1

中的try catch块中,即'myRssFeed'不是'feed',你是否看到区别? – Sam

+1

谢谢,+1并接受了答案。 – AndroidDev

0

检查您的row.xml id's.That可能导致您的空指针。