在Fragment上解析XML时出错

时间:2015-04-18 14:59:50

标签: java android

我正在使用RSS创建一个简单的NavigationDrawer读者。我使用ParserRssParserSax处理程序完成了RSS课程。执行AsynTask时我的应用程序崩溃了。我的logcat:

 java.lang.NullPointerException
            at me.apps.rss.News$LoadXMLTask.onPostExecute(CNews.java:53)
            at me.apps.rss.News$LoadXMLTask.onPostExecute(CNews.java:45)
            at android.os.AsyncTask.finish(AsyncTask.java:632)
            at android.os.AsyncTask.access$600(AsyncTask.java:177)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method) 

我的观看代码。请注意,该任务仅在我按下按钮时执行。

public class News extends android.support.v4.app.Fragment {
    private List<whatsnew> news;
    private TextView txtresult;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.lay_news, container, false);
        Button but = (Button) v.findViewById(R.id.but);

        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LoadXMLTask task1 = new LoadXMLTask();
                task1.execute("http://www.feedforall.com/sample-feed.xml");
            }
        });



        return v;
    }

    private class LoadXMLTask extends AsyncTask<String, Integer, Boolean> {
        protected Boolean doInBackground (String ... params){
            RssParserSax saxparser= new RssParserSax(params[0]);
            news = saxparser.parse();
            return true;
        }

        protected  void onPostExecute (Boolean result){
            txtresult.setText("");
            for(int i=0; i<news.size(); i++)
            {
               //nothing
            }
        }

    }
}

3 个答案:

答案 0 :(得分:1)

txtresult.setText("");

这是问题,参考可能是null

实际上,堆栈跟踪中的这两行确切地告诉您问题所在:

at me.apps.rss.News$LoadXMLTask.onPostExecute(CNews.java:53)
at me.apps.rss.News$LoadXMLTask.onPostExecute(CNews.java:45)

CNews.java中的第45,53行是抛出异常的地方。如果你看这一行,你就会知道哪个引用是null

所以解决方案是在onCreateView内添加:

 txtresult = (TextView) v.findViewById(R.id.your_id);

答案 1 :(得分:1)

onCreateView忘记初始化txtresult字段时,txtresult.setText("");为您提供了NPE

答案 2 :(得分:1)

除了未初始化的txtResult之外,此操作for(int i=0; i<news.size(); i++)也不安全,因为news也未初始化。我认为使用AsyncTask的更好方法是:

    protected Boolean doInBackground (String ... params){
        RssParserSax saxparser= new RssParserSax(params[0]);
        news = saxparser.parse(); //assuming this doesn't throw exception
        if (news != null){
         return true;
        }
        return false;
    }

    protected  void onPostExecute (Boolean result){

        if (result){
            for (int i =0; i<news.size();i++){...}
            txtresult.setText("success");
        }else{
            txtresult.setText("failed");
        }

    }