-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());
}
}
});
}
}
是否有任何問題或錯誤,我做了。請幫幫我。或者只是給我一些方向。
在此先感謝
我想你忘記初始化'error'的TextView中的onCreate活動 –
的你確定你所得到的迴應CustomHttpClient? – Zohaib
哪一行代碼會引發異常? –