-2

我是 Android 开发的新手。我开发了一个包含登录名的应用程序,必须在文本字段中传递凭据,然后它将调用 Web 服务。我正面临这个问题,因为用户和密码没有被复制到所需的位置。请帮帮我。

    package com.authorwjf.http_get;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Main extends Activity implements OnClickListener {

    EditText txtUserName;
    EditText txtPassword;

    @Override
    public void onCreate(Bundle savedInstanceState) {
         txtUserName=(EditText)this.findViewById(R.id.editText1);
         txtPassword=(EditText)this.findViewById(R.id.editText2);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.my_button).setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        Button b = (Button)findViewById(R.id.my_button);
        b.setClickable(false);
        new LongRunningGetIO().execute();
    }

    private class LongRunningGetIO extends AsyncTask <Void, Void, String> {

        protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException {
           InputStream in = entity.getContent();
             StringBuffer out = new StringBuffer();
             int n = 1;
             while (n>0) {
                 byte[] b = new byte[4096];
                 n =  in.read(b);
                 if (n>0) out.append(new String(b, 0, n));
             }
             return out.toString();
        }

        @Override
        protected String doInBackground(Void... params) {
             HttpClient httpClient = new DefaultHttpClient();
             HttpContext localContext = new BasicHttpContext();

            String user= txtUserName.getText().toString();
            String pass= txtPassword.getText().toString();

            System.out.println("USERRRR"+user);
             System.out.println(pass);

             //String user="at@ril.com";
             //String pass= "123456";

            String accessTokenQry = "{"+
                      "\"uid\":\""+user+"\","+
                      "\"password\":\""+pass+"\","+
                      "\"consumptionDeviceId\":\"fder-et3w-3adw2-2erf\","+
                      "\"consumptionDeviceName\":\"Samsung Tab\""+
                    "}";

             HttpPost httpPost = new HttpPost("http://devssg01.ril.com:8080/v2/dip/auth/login");
             httpPost.setHeader("Content-Type",
                    "application/json");
            httpPost.setHeader("X-API-Key",
                    "l7xx7914b8704b2d4b029ab9c4b1b9c66dbf");
            StringEntity input;
            try {
                input = new StringEntity(accessTokenQry);
                httpPost.setEntity(input);
            } catch (UnsupportedEncodingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }



             String text = null;
             try {
                   HttpResponse response = httpClient.execute(httpPost, localContext);
                   HttpEntity entity = response.getEntity();
                   text = getASCIIContentFromEntity(entity);
             } catch (Exception e) {
                 return e.getLocalizedMessage();
             }
             return text;
        }   

        protected void onPostExecute(String results) {
            if (results!=null) {
                EditText et = (EditText)findViewById(R.id.my_edit);
                et.setText(results);
            }
            Button b = (Button)findViewById(R.id.my_button);
            b.setClickable(true);
        }
    }
}

LogCat 输出为:

08-10 01:20:23.977: W/dalvikvm(760): threadid=14: thread exiting with uncaught exception (group=0x414c4700)
08-10 01:20:23.984: E/AndroidRuntime(760): FATAL EXCEPTION: AsyncTask #4
08-10 01:20:23.984: E/AndroidRuntime(760): java.lang.RuntimeException: An error occured while executing doInBackground()
08-10 01:20:23.984: E/AndroidRuntime(760):  at android.os.AsyncTask$3.done(AsyncTask.java:299)
08-10 01:20:23.984: E/AndroidRuntime(760):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
08-10 01:20:23.984: E/AndroidRuntime(760):  at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
08-10 01:20:23.984: E/AndroidRuntime(760):  at java.util.concurrent.FutureTask.run(FutureTask.java:239)
08-10 01:20:23.984: E/AndroidRuntime(760):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
08-10 01:20:23.984: E/AndroidRuntime(760):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
08-10 01:20:23.984: E/AndroidRuntime(760):  at java.lang.Thread.run(Thread.java:841)
08-10 01:20:23.984: E/AndroidRuntime(760): Caused by: java.lang.NullPointerException
08-10 01:20:23.984: E/AndroidRuntime(760):  at com.authorwjf.http_get.Main$LongRunningGetIO.doInBackground(Main.java:66)
08-10 01:20:23.984: E/AndroidRuntime(760):  at com.authorwjf.http_get.Main$LongRunningGetIO.doInBackground(Main.java:1)
08-10 01:20:23.984: E/AndroidRuntime(760):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-10 01:20:23.984: E/AndroidRuntime(760):  at java.util.concurrent.FutureTask.run(FutureTask.java:234)
08-10 01:20:23.984: E/AndroidRuntime(760):  ... 3 more
4

5 回答 5

1

您正在尝试EditText在加载布局之前初始化 s 。

如果要在布局上获取 EditText,则必须在加载布局后对其进行初始化。

这是正确的代码:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.my_button).setOnClickListener(this);

        txtUserName=(EditText)this.findViewById(R.id.editText1);
        txtPassword=(EditText)this.findViewById(R.id.editText2);
    }
于 2013-08-10T05:17:16.333 回答
1

用这个

               EditText textw3d =(EditText) findViewById(R.id.editText3d);
                final String  strd3d = textw3d.getText().toString();
于 2013-08-10T05:17:52.457 回答
1

我建议在您的代码中进行以下更改。

只需写以下几行

super.onCreate(savedInstanceState); 设置内容视图(R.layout.main);

多于

txtUserName=(EditText)this.findViewById(R.id.editText1); txtPassword=(EditText)this.findViewById(R.id.editText2);

于 2013-08-10T05:25:12.757 回答
1

在 setContentView 函数调用之后移动您获得对文本视图的引用的行:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    findViewById(R.id.my_button).setOnClickListener(this);

    txtUserName=(EditText)this.findViewById(R.id.editText1);
    txtPassword=(EditText)this.findViewById(R.id.editText2);
}

事实上,您需要在初始化布局中的任何小部件之前调用 setContentView,因为这是将 layout_main.xml 文件中定义的布局“加载”到您的活动中的调用。

于 2013-08-10T05:31:42.850 回答
0

非常感谢Guyz,现在问题已经解决了。特别感谢 JustWork

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        findViewById(R.id.my_button).setOnClickListener(this);

        txtUserName=(EditText)this.findViewById(R.id.editText1);
        txtPassword=(EditText)this.findViewById(R.id.editText2);
    }
于 2013-08-10T05:37:15.137 回答