-2

嗨,我对 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

任何帮助,将不胜感激。

4

2 回答 2

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);
于 2013-08-05T20:07:53.993 回答
1

我相信您的错误是由TextView t = new TextView(this);. 我不认为你想像那样实例化它。相反,只需将其保留在您的实例变量中,并通过在您的方法中TextView t;通过 ID 查找它来实例化它。onCreate那应该可以解决问题。构造TextView函数应该带一个上下文,它更像是一个抽象对象,所以使用构造函数实例化它通常最好不要这样做。

于 2013-08-05T19:48:45.770 回答