嗨,我对 android dev 还很陌生,但是我搜索了这些论坛并没有发现任何结论。我正在尝试使用 ASyncTask 在文本视图中显示来自站点的 HTML。这是我在下面使用的代码。
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
任何帮助,将不胜感激。