0

我编写了一个将调用 REST Web 服务的 Android 应用程序。首先,我尝试了一个固定变量的 Android 应用程序。我可以成功调用 Web Service 。

但是当我在 Android 应用程序中集成一个 GUI 并在变量中赋值时,在运行时我得到以下异常:

java.lang.reflect.InvocationTargetException
java.lang.NullPointerException

为什么我会得到异常?

我的程序代码是:

public class testing extends Activity {

private EditText txturl,foldername,metadata,Username,Password;

private TextView textview;

private Button btnok,btncancel;

private String url,foldname,Metadata;

private String username,password;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);        


    btnok = (Button)findViewById(R.id.btncrtfold);

    btncancel=(Button)findViewById(R.id.btnreset);


            btnok.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {
              txturl=(EditText)findViewById(R.id.txturl);

            Username=(EditText)findViewById(R.id.txtloginname);

            Password=(EditText)findViewById(R.id.txtpassword);

            foldername = (EditText)findViewById(R.id.txtfoldername);

            metadata=(EditText)findViewById(R.id.txtmetadata);

        foldname=foldername.getText().toString();

        Metadata=metadata.getText().toString();

        username=Username.getText().toString();

        url=txturl.getText().toString();

        password=Password.getText().toString();


            //HttpRequestInterceptor preemptiveAuth=login();
            String result=doprocess(url,username,password,Metadata,foldname);
            textview.setText(result);
     }});
} 

       public String doprocess(String url,String username,String password,String metadata,String fold){
            HttpResponse response=null;
        String content=null;

        //public static void login() {
              HttpRequestInterceptor preemptiveAuth = new HttpRequestInterceptor() {
                        @Override 
                        public void process(HttpRequest request,
                                HttpContext context) throws HttpException,
                                IOException {
                             AuthState authState = (AuthState) context.getAttribute(ClientContext.TARGET_AUTH_STATE);
                                CredentialsProvider credsProvider = (CredentialsProvider) context.getAttribute(
                                        ClientContext.CREDS_PROVIDER);
                                HttpHost targetHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);

                                if (authState.getAuthScheme() == null) {
                                    AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort());
                                    Credentials creds = credsProvider.getCredentials(authScope);
                                    if (creds != null) {
                                        authState.setAuthScheme(new BasicScheme());
                                        authState.setCredentials(creds);
                                    }
                             }
                        }    
                    };



        DefaultHttpClient httpclient = new DefaultHttpClient();
        url= url+"/"+username+"/"+foldername;
        HttpPut put=new HttpPut(url);
        try{
        httpclient.getCredentialsProvider().setCredentials(
                new AuthScope(null, -1),
                new UsernamePasswordCredentials(username, password));
        httpclient.addRequestInterceptor(preemptiveAuth, 0);
        HttpEntity data=new StringEntity(metadata);
        put.setEntity(data);
        put.setHeader("Content-Type","application/vnd.org.snia.cdmi.container");
        response = httpclient.execute(put);
        HttpEntity resEntity=response.getEntity();
        if(resEntity!=null)
        content = (EntityUtils.toString(resEntity));
      }catch (Exception e) {
        Log.e("PUT Exception", "java.lang.NullPointerException");
      }
        String result=response.getStatusLine().toString()+"\n"+content;

        return result;
}
4

1 回答 1

1

嗨,我认为您的 doProcess 方法不会返回任何内容,因此它会抛出

Textview 方法的 setText 上的 NullPointerException。

请确认。

于 2011-06-15T07:25:51.393 回答