【问题标题】:java.lang.NullPointerException at onPostExecute?onPostExecute 的 java.lang.NullPointerException?
【发布时间】:2012-10-22 09:56:50
【问题描述】:

我有这些类,其中我正在向服务器发送 SOAP 请求。我有我的主类、一个 AsyncTask 类、一个解析器类、一个接口。我的问题是我总是得到这个 NullpointerException。我正在尝试做的是在我的主活动中,我将向 AsyncTask 发送一个字符串,然后将 AsyncTask 中的 onPostExecute 的结果,我需要将它传递给 Parser 类,在这部分我遇到了问题。我使用的是我上一个问题中的接口,但它不起作用。

AsyncTask 类:

public class TestAsyncTask extends AsyncTask<String, Void, String> {

public TestInterface delegate = null;
String soapString = "http://-------------"; 
String serverString = "https:-----------";

@Override
protected String doInBackground(String... sString) {
String yourValue = sString[0];
String resString = null; //This will be the storage of the response.

try {

    //Uses URL and HttpURLConnection for server connection
    URL url = new URL(serverString); 
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
    urlConnection.setDoOutput(true); 
    urlConnection.setDoInput(true); 
    urlConnection.setUseCaches(false); 
    urlConnection.setChunkedStreamingMode(0); 
    urlConnection.addRequestProperty("SOAPAction", soapString); 
    urlConnection.addRequestProperty("Content-Type", "text/xml; charset=utf-8"); 
    urlConnection.addRequestProperty("Content-Length", "" + yourValue.length()); 
    urlConnection.setRequestMethod(HttpPost.METHOD_NAME); 

    // Using OutputStream and Writer to send a request to the server.
    OutputStream outputStream = urlConnection.getOutputStream(); 
    Writer writer = new OutputStreamWriter(outputStream); 
    writer.write(yourValue); 
    writer.flush(); 
    writer.close(); 

    //Using InputStream to get the response of the request from the server.
    InputStream inPutStream = urlConnection.getInputStream(); 
    BufferedReader buffReader = new BufferedReader(new InputStreamReader(inPutStream)); 
    ByteArrayBuffer byteBuffer = new ByteArrayBuffer(50);

    int resint = urlConnection.getResponseCode(); 

    while ((resint = buffReader.read()) != -1) {
        byteBuffer.append(resint); 
    }

    resString = new String(byteBuffer.toByteArray()); 

    } catch (Exception aException) {
    resString = aException.getMessage(); 
    }

    return resString; 
}


protected void onPostExecute(String aReturnValueString) {
    //I THINK HERE IS WHERE THE PROBLEM EXIST
            delegate.processFinish(aReturnValueString);
}
}

界面,这个我不确定:

public interface TestInterface {
  void processFinish(String output);
}

解析器类:

public class TestParser implements TestInterface {

TestAsyncTask a= new TestAsyncTask();
TextView oTextView[], aTextView[], pTextView[], dTextView[]; //Create a new TextView array to store the parsing results.
private LinearLayout linearLayout;
int pars;

Context contxt;

//Constructor
public TestParser(Context cContext){
    contxt = cContext; //Pass Context to constructor
}

//Getter for LinearLayout.
    public LinearLayout getLinearLayout(){
        return linearLayout;
    }
    //Setter for LinearLayout.
    public void setLinearLayout(LinearLayout aSetterLinearLayout){
        this.linearLayout = aSetterLinearLayout;
    }


    public void onCreate(Bundle savedInstanceState) {
     a.delegate = this;
    }

 public void processFinish(String output) {
    // TODO Auto-generated method stub
    linearLayout = new LinearLayout(this);

    //The try/catch statement encloses some code and is used to handle errors and exceptions that might occur in that code.
    try {

        //Use SAXParser(Simple API for XML) to handle the parsing of XML(Response). */

        SAXParserFactory saxParF = SAXParserFactory.newInstance(); 
        SAXParser sAXPar = saxParF.newSAXParser(); 
        XMLReader xMLReader = sAXPar.getXMLReader(); 

        // Create handler to handle XML Tags (GBXMLHandler extends DefaultHandler).         
        TestXMLHandler xMLHandler = new TestXMLHandler(); 
        xMLReader.setContentHandler(xMLHandler); 
        InputSource inputSource = new InputSource(new StringReader(output)); 
        xMLReader.parse(inputSource);

        TestData data = TestXMLHandler.aData; 

        int aone = data.getoName().size();
        int atwo = data.getDes().size();

        oTextView = new TextView[aone];  
        aTextView = new TextView[aone]; 
        dTextView = new TextView[atwo];
        pTextView = new TextView[atwo];

        //The for statement provides a compact way to iterate over a range of values.
        for (pars = 0; pars + 1 < atwo; pars++) {

        dTextView[pars] = new TextView(getApplicationContext()); 
        dTextView[pars].setText("Description = " + data.getDes().get(pars)); 
        linearLayout.addView(dTextView[pars]); 

        pTextView[pars] = new TextView(getApplicationContext()); 
        pTextView[pars].setText("RegularSellUnitPrice = " + data.getRegularSellUnitPrice().get(pars)); 
        linearLayout.addView(pTextView[pars]);
        }

        for (pars = 0; pars < aone; pars++){
        oTextView[pars] = new TextView(getApplicationContext()); 
        oTextView[pars].setText("OperatorName = " + data.getoName().get(pars)); 
        linearLayout.addView(oTextView[pars]);

        aTextView[pars] = new TextView(getApplicationContext()); 
        aTextView[pars].setText("type = " + data.getType().get(pars));
        linearLayout.addView(aTextView[pars]);      
        }
    } catch (Exception e) {
        System.out.println("XML Pasing Exception = " + e);
    }
}
}

【问题讨论】:

  • 你能发布堆栈跟踪吗?这是很多代码,只是被要求筛选并被告知某处有错误。
  • 对不起。在调试器中运行应用程序时。在我的 onPostExecute 中,错误存在。
  • 您的TestParser 课程不正确。首先你不应该像你一样实例化一个Activity,否则onCreate回调将不会被调用(所以delegate将为空,因此NullPointerException
  • hmm.. 我的 TestParser 不正确,您对如何纠正它有什么想法或例子吗?
  • 如果你解释你想对每个类做什么(特别是在 TestParser 类中),也许我可以推荐一些东西。

标签: android interface android-asynctask


【解决方案1】:

在我看来,您的 delegate 对象从未被实例化。

您将其声明为null,然后从不调用delegate = new TestParser()

【讨论】:

    猜你喜欢
    • 2023-03-25
    • 2015-05-04
    • 1970-01-01
    • 2018-03-30
    • 2019-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多