将android应用程序与Web服务连接时出现异常

时间:2011-06-03 11:58:03

标签: android

这是我从这里得到的编码。我在日志猫视图中得到的错误就像我在编码后提到的那样..请给我一些建议来清除它..

package com.example.firstws;



import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

public class Firstws extends Activity {
    /** Called when the activity is first created. */
    private static final String SOAP_ACTION="http://tempuri.org/CelsiusToFarenheit";
    private static final String METHOD_NAME="CelsiusToFarenheit";
    private static final String NAMESPACE="http://tempuri.org/";
    private static final String URL="http://www.w3schools.com/webservices/tempconvert.asmx";
    TextView tv=(TextView)findViewById(R.id.textView1);
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        SoapObject Request=new SoapObject(NAMESPACE,METHOD_NAME);
        Request.addProperty("Celsius","32");
        SoapSerializationEnvelope soapEnvelope=new SoapSerializationEnvelope(SoapEnvelope.VER11);
        soapEnvelope.dotNet=true;
        soapEnvelope.setOutputSoapObject(Request);
        AndroidHttpTransport aht=new AndroidHttpTransport(URL);
        try
        {
            aht.call(SOAP_ACTION, soapEnvelope);
            SoapPrimitive resultString=(SoapPrimitive)soapEnvelope.getResponse();
            tv.setText("Status:"+resultString);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        }

    }

错误:

java.lang.runtime exception: unable to instantiate the activity componentinfo{com.example.firstws/com.example.firstws.Firstws}: java.lang.NullpointerException

1 个答案:

答案 0 :(得分:1)

在致电TextView tv=(TextView)findViewById(R.id.textView1);之前,您无法执行此操作setContentView()。由于在类初始化时没有设置布局,因此将失败。在致电setContentView()

后移动作业