1

我在哪裏解析器分配空值,但是當物場被選中(飼料變量)不爲空的問題setInput設置功能。下面的代碼。任何幫助表示讚賞。空指針異常:用於Xmlpullparser分配空值與StringReader

線造成的錯誤是:xpp.setInput(新StringReader(mwordItem.getFeed()));

package com.example.wffd; 

import android.os.Bundle; 
import android.os.AsyncTask; 
import android.app.Activity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.view.Menu; 

import java.io.BufferedInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.io.StringReader; 

import java.net.URL; 
import java.net.HttpURLConnection; 
import org.xmlpull.v1.XmlPullParser; 
import org.xmlpull.v1.XmlPullParserException; 
import org.xmlpull.v1.XmlPullParserFactory; 




public class WffdhomeActivity extends Activity { 

    Button updateButton; 
    TextView wdTextView, cwTextView; 
    WordItem myWordItem ; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_wffdhome); 
     updateButton = (Button)findViewById(R.id.button1); 
     wdTextView = (TextView)findViewById(R.id.textView2); 
     cwTextView = (TextView)findViewById(R.id.textView3); 
     myWordItem= new WordItem(); 


     updateButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
     try { 
      URL url = new URL("http://www.dictionary.reference.com/wordoftheday/wotd.rss"); 
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 

      new FeedGrab(myWordItem).execute(urlConnection); 
      update_screen(myWordItem); 

      cwTextView.setText(myWordItem.getWord()); 
      wdTextView.setText(myWordItem.getDefinition()); 
     } catch (XmlPullParserException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     } 
     }); 

    } 

    private class FeedGrab extends AsyncTask<HttpURLConnection,Void,Boolean>{ 

     WordItem aWordItem; 

     public FeedGrab(WordItem mWordItem){ 

      this.aWordItem=mWordItem; 
     } 

     protected Boolean doInBackground(HttpURLConnection... urls) { 
      try { 
       InputStream in = new BufferedInputStream(urls[0].getInputStream()); 
       aWordItem.setFeed(readStream(in)); 

      } 
      catch(IOException e) 
      { 
       e.printStackTrace(); 
      } 
      finally { 

       urls[0].disconnect(); 
      } 
      return true; 
     } 

     private String readStream(InputStream is) throws IOException { 
       StringBuilder sb = new StringBuilder(); 
       BufferedReader r = new BufferedReader(new InputStreamReader(is),1000); 
       for (String line = r.readLine(); line != null; line =r.readLine()){ 
        sb.append(line); 
       } 
       is.close(); 
       return sb.toString(); 
      } 


    } 
    public void update_screen(WordItem mwordItem)throws XmlPullParserException, IOException 
    { 
     XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
     factory.setNamespaceAware(true); 
     XmlPullParser xpp = factory.newPullParser(); 
     xpp.setInput(new StringReader (mwordItem.getFeed())); 


     int eventType = xpp.getEventType(); 
     String tag_id="item"; 
     boolean in_item = false,in_description = false; 

     String tag_def="description"; 



     while (eventType != XmlPullParser.END_DOCUMENT) { 

     if((eventType == XmlPullParser.START_TAG)) 
      { 
      if(tag_id.equalsIgnoreCase(xpp.getName())) 
        { 
         in_item = true; 
        }else if(tag_def.equalsIgnoreCase(xpp.getName())) 
        { 
         in_description= true; 
        } 


      } else if(eventType == XmlPullParser.END_TAG) { 
       System.out.println("End tag "+xpp.getName()); 

      } else if((eventType == XmlPullParser.TEXT) &&(in_description & in_item)) { 
       mwordItem.processString(xpp.getText()); 
       in_description=false; 
       in_item = false; 
      } 
      eventType = xpp.next(); 
     } 
     System.out.println("End document"); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.wffdhome, menu); 
     return true; 
    } 




} 

WordItemClass:

package com.example.wffd; 

public class WordItem { 
    private String word; 
    private String definition; 
    private String feed; 

    public String getFeed() { 
     return feed; 
    } 
    public void setFeed(String feed) { 
     this.feed = feed; 
    } 

    public String getWord() { 
     return word; 
    } 
    public void setWord(String word) { 
     this.word = word; 
    } 
    public String getDefinition() { 
     return definition; 
    } 
    public void setDefinition(String definition) { 
     this.definition = definition; 
    } 
    public void processString(String text){ 
     String[] phrase = text.split(":"); 
    this.setWord(phrase[0]); 
    this.setDefinition(phrase[1]); 

    } 


} 

編輯:不知道這是堆棧跟蹤..但這裏:

Thread [<1> main] (Suspended (exception NullPointerException)) 
    <VM does not provide monitor information> 
    WffdhomeActivity.update_screen(WordItem) line: 110 
    WffdhomeActivity$1.onClick(View) line: 51 
    Button(View).performClick() line: 4204 
    View$PerformClick.run() line: 17355 
    Handler.handleCallback(Message) line: 725 
    ViewRootImpl$ViewRootHandler(Handler).dispatchMessage(Message) line: 92 
    Looper.loop() line: 137 
    ActivityThread.main(String[]) line: 5041  
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method] 
    Method.invoke(Object, Object...) line: 511 
    ZygoteInit$MethodAndArgsCaller.run() line: 793 
    ZygoteInit.main(String[]) line: 560 

**更新:使用調試器mwordItem.getfeed()是返回null,但Feed字符串包含數據。不知道這是怎麼回事

**更新@ andy256:我認爲使用aWordItem作爲myWordItem的參考和修改飼料變量我已經解決了這個問題顯然這不是我所做的。我如何從異步線程傳遞值?我嘗試通過onpostexecute傳遞價值,但沒有成功。

保護無效onPostExecute(WordItem結果){

 this.aWordItem=result; 
     return; 
    } 
+0

你可以發佈堆棧跟蹤嗎? – andy256

回答

1

堆棧跟蹤顯示

WffdhomeActivity.update_screen(WordItem)線:110
WffdhomeActivity $ 1.onClick(查看)線:51

因此,第110行的內容是null

xpp.setInput(new StringReader (mwordItem.getFeed())); 

由於xpp似乎設置,請看mwordItem。它來自第51行的呼叫,其中myWordItem通過。在發佈的代碼中,myWordItem從未設置。

所以,從您發佈的代碼:myWordItemnull

+0

據我所看到的,這只是傳遞一個'null'參考各地。你仍然需要在某個地方創建一個'WordItem':'新的WordItem(...)'。 – andy256

+0

原來,問題是,異步線程仍在運行,而我要求的值(NULL),因爲該刑法尚未完成運行。一旦我把在onPostExecute的UI update()函數,它的工作順利 –