2015-04-23 85 views
1

我從網站獲取一些inputStream(即http://developer.android.com/training/basics/network-ops/connecting.html) 我有xml解析器,並且在一個類中,我通過解析這個流來創建自己的對象xml。如果我檢查它那裏一切都很好。在我試圖從getter獲得另一個類的這個對象後,我得到空指針異常,我不明白爲什麼。爲什麼我在填充後得到空對象?

一流

public class Connection { 

private final String URLSTRING = "http://blabla.html"; 
private Xml xml; 
private Context context; 
private String result; 
private boolean connection; 

public Xml getXml() { 
    return xml; 
} 

public void setXml(Xml xml) { 
    this.xml = xml; 
} 

public String getResult() { 
    return result; 
} 

public void setResult(String result) { 
    this.result = result; 
} 

public boolean getConnection() { 
    return connection; 
} 

public void setConnection(boolean connection) { 
    this.connection = connection; 
} 

public Connection(Context context) { 
    this.context = context; 
} 
public void connect() { 
    ConnectivityManager connMgr = (ConnectivityManager) 
      context.getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); 
    if (networkInfo != null && networkInfo.isConnected()) { 
     new DownloadWebpageTask().execute(URLSTRING); 
     connection = true; 

    } else { 
     connection = false; 
    } 
} 


private class DownloadWebpageTask extends AsyncTask<String, Void, String> { 
@Override 
protected String doInBackground(String... urls) { 

    // params comes from the execute() call: params[0] is the url. 
    try { 
     return downloadUrl(urls[0]); 
    } catch (IOException e) { 
     return "Unable to retrieve web page. URL may be invalid."; 
    } 
} 
// onPostExecute displays the results of the AsyncTask. 
@Override 
protected void onPostExecute(String result) { 
    result = ""; 
} 
} 
    private String downloadUrl(String myurl) throws IOException { 
    InputStream is = null; 

    try { 
     URL url = new URL(myurl); 
     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     conn.setRequestMethod("GET"); 
     conn.setDoInput(true); 
     // Starts the query 
     conn.connect(); 
     int response = conn.getResponseCode(); 
     //Log.d(DEBUG_TAG, "The response is: " + response); 
     is = conn.getInputStream(); 


     String contentAsString ="aa"; 
     XmlParser xmlParser = new XmlParser(); 
     xml = xmlParser.getParsedXml(is); 
     return contentAsString; 
    } finally { 
     if (is != null) { 
      is.close(); 
     } 
    } 
} 

我試圖返工DownloadWebpageTask爲xml太沒有成功。 第二主Activity類,當我嘗試建立的TextView

public class MainActivity extends ActionBarActivity { 

private Button button; 
private TextView textView; 
private final String URLSTRING = "http://blabla.html"; 
private Xml xml; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    button = (Button) findViewById(R.id.button); 
    textView = (TextView) findViewById(R.id.textView2); 
    final Context context = this; 

    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Connection con = new Connection(context); 
      con.connect(); 
      if(con.getConnection()){ 
       xml = con.getXml(); 
       textView.setText(xml.getDate()); 
      }else Toast.makeText(getApplicationContext(), 
        "no connection", Toast.LENGTH_LONG).show(); 

     } 

    }); 
} 

錯誤appeares。 感謝您的建議。 編輯:logcat

FATAL EXCEPTION: main 
java.lang.NullPointerException 
     at com.example.blackess.connectiontest.MainActivity$1.onClick(MainActivity.java:48) 
     at android.view.View.performClick(View.java:2485) 
     at android.view.View$PerformClick.run(View.java:9080) 
     at android.os.Handler.handleCallback(Handler.java:587) 
     at android.os.Handler.dispatchMessage(Handler.java:92) 
     at android.os.Looper.loop(Looper.java:130) 
     at android.app.ActivityThread.main(ActivityThread.java:3687) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:507) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
     at dalvik.system.NativeStart.main(Native Method) 
+0

發佈logcat?提供實際錯誤的 – Baby

+0

將幫助人們協助 – nomistic

+0

「MainActivity」的第48行是什麼? – Emmanuel

回答

1

這是一個併發問題。

的方法connect()

public void connect() { 
    ... 
    if (networkInfo != null && networkInfo.isConnected()) { 
    new DownloadWebpageTask().execute(URLSTRING); 
    connection = true; 
    } 
} 

不等待DownloadWebpageTask的AsyncTask完成。它立即返回。

你可以做的是做一個監聽器。將這個裏面Connection

public interface ConnectionListener { 
    public void onGotXML(XMl xml); 
} 

添加它在構造函數中:

ConnectionListener listener; 
public Connection(Context context, ConnectionListener listener) { 
    this.context = context; 
    this.listener = listener; 
} 

您在postExecute

@Override 
protected void onPostExecute(String result) { 
    listener.onGotXML(this.xml); 
} 

致電然後在你的活動實現這個監聽器:

Connection con; 
TextView textView;  

public void onCreate(){ 
... 
    textView = (TextView) findViewById(R.id.textView2); 
    con = new Connection(context, new Connection.ConnectionListener(){ 

    @Override 
    public void onGotXML(XMl xml){ 
     if(xml != null){ 
     textView.setText(xml.getDate()); 
     }else{ 
      Toast.makeText(getApplicationContext(), 
       "no connection", Toast.LENGTH_LONG).show(); 
     } 
    } 
    }); 

    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
     con.connect(); 
     } 

    }); 

} 
+0

我該怎麼做,讓這項任務完成他的負載? – previ

+0

只有吐司在錯誤的地方,但我可以解決這個問題。 完美!謝謝! – previ

相關問題