所以我试图改编本教程:http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/
到我自己的项目。这是一个基本的寄存器&使用SQLite& amp;登录Android / Java通过PHP排到SQL服务器。 我可以把教程写成书面工作没问题。然而,在将它转移到我的项目时,我遇到了崩溃;
05-12 01:18:05.763 1965-1965/com.disclosure_scots.disclosure_scots E/AndroidRuntime? FATAL EXCEPTION: main
Process: com.disclosure_scots.disclosure_scots, PID: 1965
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.disclosure_scots.disclosure_scots.AppController.addToRequestQueue(com.android.volley.Request, java.lang.String)' on a null object reference
at com.disclosure_scots.disclosure_scots.RegisterActivity.registerUser(RegisterActivity.java:194)
at com.disclosure_scots.disclosure_scots.RegisterActivity.access$300(RegisterActivity.java:34)
at com.disclosure_scots.disclosure_scots.RegisterActivity$1.onClick(RegisterActivity.java:90)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
它引用的RegisterActivity中的行也是;
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
所涉及的活动是;
RegisterActivity
package com.disclosure_scots.disclosure_scots;
import android.app.ProgressDialog;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.disclosure_scots.disclosure_scots.AppConfig;
import com.disclosure_scots.disclosure_scots.AppController;
import com.disclosure_scots.disclosure_scots.SQLiteHandler;
import com.disclosure_scots.disclosure_scots.SessionManager;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
public class RegisterActivity extends ActionBarActivity {
private static final String TAG = RegisterActivity.class.getSimpleName();
private Button btnRegister;
private EditText inputFullName;
private EditText inputEmail;
private EditText inputPassword;
private EditText inputTel_No;
private EditText inputHome_Add;
private EditText inputPostcode;
private CheckBox inputPostal;
private ProgressDialog pDialog;
private SessionManager session;
private SQLiteHandler db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
inputFullName = (EditText) findViewById(R.id.editTextName);
inputEmail = (EditText) findViewById(R.id.editTextEmail);
inputPassword = (EditText) findViewById(R.id.editTextPass);
inputTel_No = (EditText) findViewById(R.id.editTextPhone);
inputHome_Add = (EditText) findViewById(R.id.editTextPost);
inputPostcode = (EditText) findViewById(R.id.editTextPostcode);
inputPostal = (CheckBox) findViewById(R.id.PostalcheckBox);
btnRegister = (Button) findViewById(R.id.btnRegister);
// Progress dialog
pDialog = new ProgressDialog(this);
pDialog.setCancelable(false);
// Session manager
session = new SessionManager(getApplicationContext());
// SQLite database handler
db = new SQLiteHandler(getApplicationContext());
// Check if user is already logged in or not
if (session.isLoggedIn()) {
// User is already logged in. Take him to Legal disclaimer activity
Intent intent = new Intent(RegisterActivity.this,
LegalDiscActivity.class);
startActivity(intent);
finish();
}
// Register Button Click event
btnRegister.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String name = inputFullName.getText().toString();
String email = inputEmail.getText().toString();
String password = inputPassword.getText().toString();
if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty()) {
registerUser(name, email, password);
} else {
Toast.makeText(getApplicationContext(),
"Please enter your details!", Toast.LENGTH_LONG)
.show();
}
}
});
// Link to Login Screen
/*btnLinkToLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
LoginActivity.class);
startActivity(i);
finish();
}
});*/
}
/**
* Function to store user in MySQL database will post params(tag, name,
* email, password) to register url
* */
private void registerUser(final String name, final String email,
final String password) {
// Tag used to cancel the request
String tag_string_req = "req_register";
pDialog.setMessage("Registering ...");
showDialog();
StringRequest strReq = new StringRequest(Method.POST,
AppConfig.URL_REGISTER, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Register Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
if (!error) {
// User successfully stored in MySQL
// Now store the user in sqlite
String uid = jObj.getString("uid");
JSONObject user = jObj.getJSONObject("user");
String name = user.getString("name");
String email = user.getString("email");
String created_at = user
.getString("created_at");
// Inserting row in users table
db.addUser(name, email, uid, created_at);
// Launch login activity
Intent intent = new Intent(
RegisterActivity.this,
LoginActivity.class);
startActivity(intent);
finish();
} else {
// Error occurred in registration. Get the error
// message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Registration Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting params to register url
Map<String, String> params = new HashMap<String, String>();
params.put("tag", "register");
params.put("name", name);
params.put("email", email);
params.put("password", password);
return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
private void showDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hideDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_register, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
AppController 类
package com.disclosure_scots.disclosure_scots;
/**
* Created by Administrator on 09/05/2015.
*/
import android.app.Application;
import android.text.TextUtils;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();
private RequestQueue mRequestQueue;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
我已多次使用它,但无法说明为什么我在复制版本中获取null对象而原始工作正常。 任何帮助表示赞赏。
答案 0 :(得分:1)
这很可能是因为您没有在清单中注册AppController,因此getInstance
返回null。
答案 1 :(得分:1)
你需要在你的清单中添加这一行(可能你错过了这一行,所以它返回空值)
<application
android:name="(your app package).app.AppController" // <--this line
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
...../>