android java.lang.nullpointerexception,Android引起:java.lang.NullPointerException

本文讲述了开发者在Android应用中遇到的NullPointerException问题,主要集中在ActivityOwner类的 AttemptLogin AsyncTask任务中。作者通过检查并修复了EditText可能为null的问题,提供了解决方案和Logcat日志分析。
摘要由CSDN通过智能技术生成

我现在编辑了…我检查布局和初始化但仍然是相同的错误..它可能是日食错误?

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 {

@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 params = new ArrayList();

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)

解决方法:

A.B.C形式的行上的空指针异常意味着点左侧的某些内容为空.在这个例子中:

> A无效;

>或A.B为空.

没有其他原因.因此,由于textView.getText()永远不会返回null,唯一的可能是用户为null.

标签:android,eclipse,android-asynctask,nullpointerexception

来源: https://codeday.me/bug/20191005/1856245.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值