2013-08-05 42 views
-2

嗨我很新的Android開發,但我已經搜索這些論壇,沒有發現任何結論。我正嘗試使用ASyncTask在文本視圖中顯示站點中的HTML。以下是我在下面使用的代碼。從網址使用異步的Html,Android

package com.uad1001.theredux; 
//imports  
public class Htmlfromurl extends Activity { 
    Button btnSend; 
    EditText PhoneNo; 
    TextView t = new TextView(this); 
    String html; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_htmlfromurl); 
     t =(TextView)findViewById(R.id.IPDisplay); 
     // will implement later 
      //PhoneNo = (EditText) findViewById(R.id.phone); 
     //btnSend = (Button) findViewById(R.id.send); 
     String IPurl = "http://myexternalip.com/raw"; //URL I want to scrape 

     new event().execute(IPurl); 
    } 

    public class event extends AsyncTask<String, Void, String>{ 
     @Override 
     protected String doInBackground(String...url) { 
      // TODO Auto-generated method stub 
      String urls = url[0]; 
      try 
      { 
       Thread.sleep(4000); 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       HttpGet httpGet = new HttpGet(urls); 
       ResponseHandler<String> resHandler = new BasicResponseHandler(); 
       String page = httpClient.execute(httpGet, resHandler); 
       return page; 
      } 
      catch (ClientProtocolException e) 
      { 
       e.printStackTrace(); 
       return "Error"; 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
       return "Error"; 
      } 
      catch (InterruptedException e) 
      { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       return "Error"; 
      } 

     } 
     @Override 
     protected void onPostExecute(String page){ 

      t.setText(page); 
     } 
    } 

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

該程序即時崩潰。在logcat中顯示: (也在http://pastebin.com/3sTENx2q

08-05 19:38:54.082: W/dalvikvm(11472): threadid=1: thread exiting with uncaught exception (group=0x410942a0) 
08-05 19:38:54.092: E/AndroidRuntime(11472): FATAL EXCEPTION: main 
08-05 19:38:54.092: E/AndroidRuntime(11472): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.uad1001.theredux/com.uad1001.theredux.Htmlfromurl}: java.lang.NullPointerException 
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2016) 
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117) 
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.app.ActivityThread.access$700(ActivityThread.java:134) 
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218) 
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.os.Handler.dispatchMessage(Handler.java:99) 
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.os.Looper.loop(Looper.java:137) 
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.app.ActivityThread.main(ActivityThread.java:4867) 
08-05 19:38:54.092: E/AndroidRuntime(11472): at java.lang.reflect.Method.invokeNative(Native Method) 
08-05 19:38:54.092: E/AndroidRuntime(11472): at java.lang.reflect.Method.invoke(Method.java:511) 
08-05 19:38:54.092: E/AndroidRuntime(11472): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007) 
08-05 19:38:54.092: E/AndroidRuntime(11472): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774) 
08-05 19:38:54.092: E/AndroidRuntime(11472): at dalvik.system.NativeStart.main(Native Method) 
08-05 19:38:54.092: E/AndroidRuntime(11472): Caused by: java.lang.NullPointerException 
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.content.ContextWrapper.getResources(ContextWrapper.java:81) 
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.view.View.<init>(View.java:3296) 
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.view.View.<init>(View.java:3364) 
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.widget.TextView.<init>(TextView.java:584) 
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.widget.TextView.<init>(TextView.java:579) 
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.widget.TextView.<init>(TextView.java:575) 
08-05 19:38:54.092: E/AndroidRuntime(11472): at com.uad1001.theredux.Htmlfromurl.<init>(Htmlfromurl.java:23) 
08-05 19:38:54.092: E/AndroidRuntime(11472): at java.lang.Class.newInstanceImpl(Native Method) 
08-05 19:38:54.092: E/AndroidRuntime(11472): at java.lang.Class.newInstance(Class.java:1319) 
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.app.Instrumentation.newActivity(Instrumentation.java:1068) 
08-05 19:38:54.092: E/AndroidRuntime(11472): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2007) 
08-05 19:38:54.092: E/AndroidRuntime(11472): ... 11 more 

任何幫助,將不勝感激。

+1

您的logcat在第23行顯示一個空指針。哪一行是? – Geobits

+0

第23行是 TextView t = new TextView(this); Sweet修復了這個問題謝謝 –

回答

1

我相信你的錯誤是由TextView t = new TextView(this);造成的。我不認爲你想要像那樣實例化它。相反,將它作爲TextView t;保留在您的實例變量中,並通過在您的onCreate方法中通過按ID查找它來實例化它。這應該可以解決問題。 TextView構造函數應該採用Context,它更像是一個抽象對象,所以使用構造函數來實例化它通常最好不要這樣做。

1

您可以使用droidQuery來處理最繁重的工作中給你:

public void includeHTML(TextView t, String url) { 

    $.ajax(new AjaxOptions().url("http://myexternalip.com/raw") 
          .type("get") 
          .dataType("html") 
          .context(this) 
          .success(new Function() { 
           @Override 
           public void invoke($ droidQuery, Object... params) { 
            $.with(t).html((String) params[0]); 
           } 
          }) 
          .error(new Function() { 
           @Override 
           public void invoke($ droidQuery, Object... params) { 
            AjaxError error = (AjaxError) params[0]; 
            Log.e("$", "Error " + error.status + ": " + error.error); 
           } 
          })); 
} 

然後您只要致電:

includeHTML(t, url); 

如果你想4秒的延遲,你可以使用:

$.setTimeout(new Function() { 
    @Override 
    public void invoke($ droidQuery, Object... params) { 
     runOnUiThread(new Runnable() { 
      includeHTML(t, url); 
     }); 
    } 
}, 4000);