我正在尝试使用PHP和MYSQL为Android创建一个登录应用程序,但每当我启动它时它都会崩溃。我已经阅读了类似的问题,但我仍然无法查明错误
logcat报告
09-25 15:34:07.722: E/AndroidRuntime(2515): FATAL EXCEPTION: main
09-25 15:34:07.722: E/AndroidRuntime(2515): Process: com.learn2crack.tab, PID: 2515
09-25 15:34:07.722: E/AndroidRuntime(2515): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.learn2crack.tab/com.learn2crack.tab.Login}: java.lang.NullPointerException
09-25 15:34:07.722: E/AndroidRuntime(2515): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
09-25 15:34:07.722: E/AndroidRuntime(2515): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
09-25 15:34:07.722: E/AndroidRuntime(2515): at android.app.ActivityThread.access$800(ActivityThread.java:135)
09-25 15:34:07.722: E/AndroidRuntime(2515): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
09-25 15:34:07.722: E/AndroidRuntime(2515): at android.os.Handler.dispatchMessage(Handler.java:102)
09-25 15:34:07.722: E/AndroidRuntime(2515): at android.os.Looper.loop(Looper.java:136)
09-25 15:34:07.722: E/AndroidRuntime(2515): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-25 15:34:07.722: E/AndroidRuntime(2515): at java.lang.reflect.Method.invokeNative(Native Method)
09-25 15:34:07.722: E/AndroidRuntime(2515): at java.lang.reflect.Method.invoke(Method.java:515)
09-25 15:34:07.722: E/AndroidRuntime(2515): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-25 15:34:07.722: E/AndroidRuntime(2515): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-25 15:34:07.722: E/AndroidRuntime(2515): at dalvik.system.NativeStart.main(Native Method)
09-25 15:34:07.722: E/AndroidRuntime(2515): Caused by: java.lang.NullPointerException
09-25 15:34:07.722: E/AndroidRuntime(2515): at com.learn2crack.tab.Login.onCreate(Login.java:76)
09-25 15:34:07.722: E/AndroidRuntime(2515): at android.app.Activity.performCreate(Activity.java:5231)
09-25 15:34:07.722: E/AndroidRuntime(2515): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-25 15:34:07.722: E/AndroidRuntime(2515): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
我的login.java 包com.learn2crack.tab;
public class Login extends Activity implements OnClickListener {
private EditText user, pass;
private Button mSubmit, mRegister;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
// php login script location:
private static final String LOGIN_URL = "http://10.0.2.2/webservice/login.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// setup input fields
user = (EditText) findViewById(R.id.username);
pass = (EditText) findViewById(R.id.pword);
// setup buttons
mSubmit = (Button) findViewById(R.id.login);
mRegister = (Button) findViewById(R.id.register);
// register listeners
mSubmit.setOnClickListener(this);
mRegister.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.login:
new AttemptLogin().execute();
break;
case R.id.register:
Intent i = new Intent(this, Register.class);
startActivity(i);
break;
default:
break;
}
}
class AttemptLogin extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String username = user.getText().toString();
String password = pass.getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(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(Login.this);
Editor edit = sp.edit();
edit.putString("username", username);
edit.commit();
Intent i = new Intent(Login.this, MainActivity.class);
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;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null) {
Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}
PHP脚本
$ _POST [&#39;用户名&#39;] ); 尝试{ $ stmt = $ db-&gt; prepare($ query); $ result = $ stmt-&gt; execute($ query_params); } catch(PDOException $ ex){ //为了进行测试,您可以使用模具和消息。 // die(&#34;无法运行查询:&#34;。$ ex-&gt; getMessage()); //或者只是使用它来使用这个来产生JSON数据: $ response [&#34; success&#34;] = 0; // $ response [&#34; message&#34;] =&#34; Database Error1。请再试一次!&#34 ;; 死亡(json_encode($响应)); } //这将是确定用户信息是否正确的变量。 //我们将其初始化为false。 $ validated_info = false; //从查询中获取所有行 $ row = $ stmt-&gt; fetch(); if($ row){ //如果我们加密密码,我们会在这里解密,但在我们的例子中我们只是 //比较两个密码 if($ _POST [&#39;密码&#39;] === $ row [&#39;密码&#39;]){ $ login_ok = true; } } //如果用户成功登录,我们会将其发送到私有成员页面 //否则,我们会显示登录失败的消息并再次显示登录表单 if($ login_ok){ $ response [&#34; success&#34;] = 1; // $ response [&#34; message&#34;] =&#34;登录成功!&#34 ;; 死亡(json_encode($响应)); } else { $ response [&#34; success&#34;] = 0; // $ response [&#34; message&#34;] =&#34;凭证无效!&#34 ;; 死亡(json_encode($响应)); } } else { ?&GT;答案 0 :(得分:0)
使用分步执行,并检查您的资源。
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// setup input fields
user = (EditText) findViewById(R.id.username);
pass = (EditText) findViewById(R.id.pword);
// setup buttons
mSubmit = (Button) findViewById(R.id.login);
mRegister = (Button) findViewById(R.id.register);
// register listeners
mSubmit.setOnClickListener(this);
mRegister.setOnClickListener(this);
}
在OnCreate上切换断点。
检查:
// setup buttons
mSubmit = (Button) findViewById(R.id.login);
mRegister = (Button) findViewById(R.id.register);