0

我编写程序,程序应该给出文本框的名称、电子邮件等并发送到一个 url,但是程序已经停止并且运行时出现 NullPointerException 错误。

我写了我的porgram的代码。请阅读并帮助我。谢谢

SignUpAsync.java:

public class SignUpAsync extends AsyncTask<List<NameValuePair>,Void,String>{
@Override
protected String doInBackground(List<NameValuePair>... nameValuePairs) {

    HttpPost httpPost=new HttpPost("http://myURL.com");
    HttpClient httpClient=new DefaultHttpClient();
    HttpResponse httpRespons = null;

    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs[0]));
        httpRespons=httpClient.execute(httpPost);
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    HttpEntity entity=httpRespons.getEntity();
    InputStream is = null;
    try {
        is=entity.getContent();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return convertStreamToString(is);

}

private String convertStreamToString(InputStream is){
    BufferedReader bfReader=new BufferedReader(new InputStreamReader(is));
    StringBuilder sb=new StringBuilder();
    String line="";
    try {
        while((line=bfReader.readLine())!=null)
            //
            sb.append(line);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        is.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return sb.toString();

}   
}

注册.java:

public class SignUp extends Activity{

private Button signUp;
private EditText userName;
private EditText userEmail;
private EditText userPass;
private EditText userPasscode;
private EditText userPhone;

@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rewards_signup);

    initView();

    String[] params = null;
    params[0]=userName.getText().toString();
    params[1]=userEmail.getText().toString();
    params[2]=userPass.getText().toString();
    params[3]=userPasscode.getText().toString();
    params[4]=userPhone.getText().toString();

    List<NameValuePair> nameValuePairs=new ArrayList<NameValuePair>(5);
    nameValuePairs.add(new BasicNameValuePair("User Name",params[0]));
    nameValuePairs.add(new BasicNameValuePair("User Email Id",params[1]));
    nameValuePairs.add(new BasicNameValuePair("User Password",params[2]));
    nameValuePairs.add(new BasicNameValuePair("User Passcode",params[3]));
    nameValuePairs.add(new BasicNameValuePair("User Phone Number",params[4]));

    AsyncTask<List<NameValuePair>, Void, String> async;
    async=new SignUpAsync().execute(nameValuePairs);
    try {
        String result=async.get();
        if(result!=null)
            Log.i("result", result);
        else
            Log.i("result", "null");
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ExecutionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
private void initView(){
    signUp=(Button)findViewById(R.id.signup_id);
    userName=(EditText)findViewById(R.id.name_id);
    userEmail=(EditText)findViewById(R.id.email_id);
    userPass=(EditText)findViewById(R.id.password_id);
    userPasscode=(EditText)findViewById(R.id.pincode_id);
    userPhone=(EditText)findViewById(R.id.phonnum_id);
}
}

日志猫:

12-05 08:46:00.063: W/dalvikvm(15436): threadid=1: thread exiting with uncaught exception (group=0x40a7e1f8)
12-05 08:46:00.071: E/AndroidRuntime(15436): FATAL EXCEPTION: main
12-05 08:46:00.071: E/AndroidRuntime(15436): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.example.loyaltier/org.example.rewards.SignUp}: java.lang.NullPointerException
12-05 08:46:00.071: E/AndroidRuntime(15436):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
12-05 08:46:00.071: E/AndroidRuntime(15436):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
12-05 08:46:00.071: E/AndroidRuntime(15436):    at android.app.ActivityThread.access$600(ActivityThread.java:122)
12-05 08:46:00.071: E/AndroidRuntime(15436):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
12-05 08:46:00.071: E/AndroidRuntime(15436):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-05 08:46:00.071: E/AndroidRuntime(15436):    at android.os.Looper.loop(Looper.java:137)
12-05 08:46:00.071: E/AndroidRuntime(15436):    at android.app.ActivityThread.main(ActivityThread.java:4340)
12-05 08:46:00.071: E/AndroidRuntime(15436):    at java.lang.reflect.Method.invokeNative(Native Method)
12-05 08:46:00.071: E/AndroidRuntime(15436):    at java.lang.reflect.Method.invoke(Method.java:511)
12-05 08:46:00.071: E/AndroidRuntime(15436):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-05 08:46:00.071: E/AndroidRuntime(15436):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-05 08:46:00.071: E/AndroidRuntime(15436):    at dalvik.system.NativeStart.main(Native Method)
12-05 08:46:00.071: E/AndroidRuntime(15436): Caused by: java.lang.NullPointerException
12-05 08:46:00.071: E/AndroidRuntime(15436):    at org.example.rewards.SignUp.onCreate(SignUp.java:35)
12-05 08:46:00.071: E/AndroidRuntime(15436):    at android.app.Activity.performCreate(Activity.java:4465)
12-05 08:46:00.071: E/AndroidRuntime(15436):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
12-05 08:46:00.071: E/AndroidRuntime(15436):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
12-05 08:46:00.071: E/AndroidRuntime(15436):    ... 11 more
12-05 08:46:01.579: I/Process(15436): Sending signal. PID: 15436 SIG: 9

干杯。

4

4 回答 4

3

您需要初始化params

String[] params = new String[5];

也明白AsyncTask#get()这一行:

String result=async.get();

如文档中所述,“如有必要,等待计算完成,然后检索其结果。” 将阻塞主线程,直到 AsyncTask 完成。这违背了 AsyncTask 的目的......

于 2012-12-05T05:33:16.783 回答
1

您试图在不初始化参数的情况下为参数赋值。

这是问题所在

    String[] params = null;
    params[0]=userName.getText().toString();

试试这个

     String params[] = new String[5];
于 2012-12-05T05:40:06.920 回答
0

试试这个方法

    SignUpAsync async = new SignUpAsync();
    async.execute(nameValuePairs);
    try {
        String result = async.get();
        if(result!=null)
            Log.i("result", result);
        else
            Log.i("result", "null");
于 2012-12-05T05:44:02.053 回答
0

将此方法放在 Async Task 类中

        protected void onPostExecute(String result) {
        // TODO Auto-generated method stub

         if(result!=null)
                         Log.i("result", result);
                     else
                        Log.i("result", "null")
    }

You can not get result from your activity class.

于 2012-12-05T05:44:45.113 回答