-2

我对android编程有疑问。我是这方面的新手,所以不要评判。

我建立了一个登录表单,当我尝试登录时,我进入了用户名文本框 java.lang.NullPointerException。这是代码:

import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.app.Activity;
import android.os.Bundle;
import android.view.View; 
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.content.Context;
import android.content.Intent;

public class Login extends Activity {
EditText un,pw;
TextView error;
Button ok;
Button reg;
private static Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    un=(EditText)findViewById(R.id.editText1);
    pw=(EditText)findViewById(R.id.editText2);
    ok=(Button)findViewById(R.id.button1);
    reg=(Button)findViewById(R.id.button2);
    ok.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

                            // TODO Auto-generated method stub



    ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
    postParameters.add(new BasicNameValuePair("username", un.getText().toString()));

    postParameters.add(new BasicNameValuePair("password", pw.getText().toString()));



    String response = null;

    try {

    response = CustomHttpClient.executeHttpPost("<HTTP Request site>", postParameters);

    String res=response.toString();

    res= res.replaceAll("\\s+","");

    if(res.equals("1")) { 
        error.setText("Welcome to ...");
        Intent i = new Intent(context, Welcome.class);
        startActivity(i);
    }

    else

    error.setText("Sorry!! Incorrect Username or Password");

    } catch (Exception e) {

    un.setText(e.toString());

                    }
                }
            });
        }
    }

我有什么问题或错误。请帮我。或者只是给我一些指示。

提前致谢

4

2 回答 2

0

记住也要初始化你的errorTextView :)

于 2013-01-10T20:24:30.683 回答
0

您收到错误消息,因为您的 TextViewerror从未初始化。将以下行添加到您的onCreate():

error=(TextView)findViewById(R.id.<textView_ID);
于 2013-01-10T20:24:31.653 回答