簡體   English   中英

ListFragment NullPointerException錯誤

[英]ListFragment NullPointerException error

我創建了一個ListFragment ,它打算用RSS數據填充。 唯一的問題是我收到強制關閉我的應用程序的NullPointerException 這是LogCat:

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);

    }
}

   }

該錯誤指向以下代碼:

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

查找NullPointerException相對容易,只需檢查您引用的每個變量即可。 例如,我看不到實例化feed ,而是在此處聲明它:

private RSSFeed feed = null;

但是它永遠不會收到null以外的null ,在某些地方您需要像feed = new RSSFeed();這樣的代碼feed = new RSSFeed(); ...(您是要使用myRSSFeed嗎?)

檢查您的row.xml id,這可能導致您的空指針。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM