无法访问不同类中的方法/变量

时间:2013-08-15 00:19:51

标签: java android

我正在尝试将此链接的主体存储在String HTML中,然后将其加载到WebView。 (使用Jsoup解析代码)。当我按下按钮时,调试器会向我显示 String HTML = re.fixCode(); 并显示“Debug Current Instruction Pointer”。

我尝试了很多东西,比如在方法中传递字符串并传递字符串。但同样的事情一遍又一遍地发生。我的结论是它没有将方法/变量传递给我的其他类? (希望你能理解最后一点)

如果是这种情况,我该如何访问它们?通过意图?

所以要清楚,我想获取正文,将其传递给字符串,然后将其加载到WebView中。谢谢!

主要课程

public class MainActivity extends Activity {

    private String uri = "http://www.novasoftware.se/webviewer/%28S%28muz0tu55twfd43zkqrlejb55%29%29/design1.aspx?schoolid=18200&code=83310";
    private RequestTask re;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button b = (Button) findViewById(R.id.button1);
        final WebView web = (WebView) findViewById(R.id.webView1);
        WebSettings webSettings = web.getSettings();
        webSettings.setJavaScriptEnabled(true);



        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new RequestTask().execute(uri);
                loadWeb();
            }
        });

    }

    private void loadWeb() {
        **String HTML = re.fixCode();**
        System.out.println(HTML);
        //      web.loadData(re.fixedHtml, "text/html", null);
    }
}

RequestTask类

package com.dir.schema;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import android.os.AsyncTask;

public class RequestTask extends AsyncTask<String, String, String>{

    public String html, fixedHtml;

    @Override
    protected String doInBackground(String... uri) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        String responseString = null;
        try {
            response = httpclient.execute(new HttpGet(uri[0]));
            StatusLine statusLine = response.getStatusLine();
            if(statusLine.getStatusCode() == HttpStatus.SC_OK){
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                responseString = out.toString();
            } else{
                //Closes the connection.
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (ClientProtocolException e) {
            //TODO Handle problems..
        } catch (IOException e) {
            //TODO Handle problems..
        }
        return responseString;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        //Do anything with response..
        html = result;
        fixCode();
    }

    public String fixCode() {
        Document doc = Jsoup.parse(html);
        return fixedHtml = doc.body().toString();

    }

    }

1 个答案:

答案 0 :(得分:0)

您正在调用 re.fixCode(),但重新尚未设置为任何内容。我希望你现在得到一个空指针错误。

我认为您想要重写 onClick()方法,如下所示:

public void onClick(View v) {
    re = new RequestTask();
    re.execute(uri);
    loadWeb();
}