gpt4 book ai didi

java - 连接android到wamp服务器,出现空指针异常

转载 作者:太空宇宙 更新时间:2023-11-04 13:28:52 26 4
gpt4 key购买 nike

我正在开发第一个连接到外部数据库的应用程序。我目前正在使用 WAMP 作为数据库,并且正在完成一个为应用程序进行非常基本的登录的教程。我遇到的问题是寄存器 Activity 中的空指针异常。这是我的日志:

09-03 14:13:00.133  14945-14945/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.gps.gpsgametest2, PID: 14945
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.gps.gpsgametest2.app.AppController.addToRequestQueue(com.android.volley.Request, java.lang.String)' on a null object reference
at com.example.gps.gpsgametest2.RegisterActivity.registerUser(RegisterActivity.java:185)
at com.example.gps.gpsgametest2.RegisterActivity.access$300(RegisterActivity.java:31)
at com.example.gps.gpsgametest2.RegisterActivity$1.onClick(RegisterActivity.java:81)
at android.view.View.performClick(View.java:5191)
at android.view.View$PerformClick.run(View.java:20916)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5972)
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:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

这是我收到错误的类,我评论了显示错误的 3 行:

public class RegisterActivity extends Activity { //LINE 31, GETTING AN ERROR
private static final String TAG = RegisterActivity.class.getSimpleName();
private Button btnRegister;
private Button btnLinkToLogin;
private EditText inputFullName;
private EditText inputEmail;
private EditText inputPassword;
private ProgressDialog pDialog;
private SessionManager session;
private SQLiteHandler db;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
StrictMode.enableDefaults();

inputFullName = (EditText) findViewById(R.id.name);
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
btnRegister = (Button) findViewById(R.id.btnRegister);
btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);

// 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 main activity
Intent intent = new Intent(RegisterActivity.this,
MainActivity.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);
} // ^^ THAT IS LINE 81 WHERE THERE IS AN ERROR
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;
}

};

//BELOW IS LINE 185 WHERE THERE IS AN ERROR
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}

private void showDialog() {
if (!pDialog.isShowing())
pDialog.show();
}

private void hideDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}

我不知道为什么这个结果为空。当我第一次开始本教程时,我挑选了其中的某些部分。现在这几乎是逐字逐句的,但仍然不起作用。我什至尝试下载 tut 代码并运行它,它也会引发相同的异常。我已经盯着这个问题看了大约两天了,我想我可能需要一双新的眼睛来告诉我我做错了什么。欢迎任何提示!提前致谢!

编辑:这是AppController类

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);
}
}

}

最佳答案

我通常会将 get Instance 类写为

public static synchronized AppController getInstance() 
{
if(null == mInstance){mInstance = new AppController()}
return mInstance;
}

我会在方法中添加一个写入日志来查看它是否为空

关于java - 连接android到wamp服务器,出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32383152/

26 4 0
文章推荐: jquery - IE10 - 需要将 CSS 光标更改为 "pointer"在 contenteditable
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com