我現在編輯它...我檢查佈局和初始化,但仍然是相同的錯誤..它可能是月食錯誤?Android導致:java.lang.NullPointerException
public class ActivityOwner extends Activity implements OnClickListener {
EditText ownerUser, ownerPass;
private Button btnLogin;
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
private static final String OWNER_LOGIN_URL = "http://192.168.2.5/idot/owner_login.php";
// ---JSON element ids from response of php script
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_owner);
ownerUser = (EditText) findViewById(R.id.owner_login_username);
ownerPass = (EditText) findViewById(R.id.owner_login_password);
TextView tvLinkToRegister = (TextView) findViewById(R.id.link_to_register);
tvLinkToRegister.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
Intent registrationForm = new Intent(ActivityOwner.this,
ActivityOwnerRegister.class);
startActivity(registrationForm);
}
});
btnLogin = (Button) findViewById(R.id.btnOwnerLogin);
btnLogin.setOnClickListener(this);
}
@Override
public void onClick(View v) {
new AttemptLogin().execute();
}
class AttemptLogin extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ActivityOwner.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// Check for success tag
int success;
String username = ownerUser.getText().toString();
String password = ownerPass.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("owner_username", username));
params.add(new BasicNameValuePair("owner_password", password));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(OWNER_LOGIN_URL,
"POST", params);
// check your log for json response
Log.d("Login attempt", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("Login Successful!", json.toString());
// save user data
SharedPreferences sp = PreferenceManager
.getDefaultSharedPreferences(ActivityOwner.this);
Editor edit = sp.edit();
edit.putString("owner_username", username);
edit.commit();
Intent i = new Intent(ActivityOwner.this,
ActivityOwnerSuccessLogin.class);
pDialog.dismiss();
finish();
startActivity(i);
return json.getString(TAG_MESSAGE);
} else {
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (result != null) {
Toast.makeText(ActivityOwner.this, result, Toast.LENGTH_SHORT)
.show();
}
}
}
}
------ logcat的-------
11-14 21:30:52.517: E/AndroidRuntime(18929): Caused by: java.lang.NullPointerException
11-14 21:30:52.517: E/AndroidRuntime(18929): at ph.idot.owner.ActivityOwner$AttemptLogin.doInBackground(ActivityOwner.java:88)
你在哪裏設置'user'?它可能是'null'。 – nfechner
「用戶」變量可能爲空,因此您無法成功調用getText()。 – Kuffs
如何聲明並初始化「用戶」? – Avijit