英文:
application crashes due to adding data to the database
问题
我认为问题出在 "NetworkCalls" 部分。
NetworkCalls:
package com.test.test;
import android.content.Context;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
public class NetworkCalls {
private RequestQueue requestQueue;
private static Context context;
private static NetworkCalls ourInstance = new NetworkCalls();
public static NetworkCalls getInstance() {
return ourInstance;
}
private NetworkCalls() {
}
public RequestQueue getRequestQueue(){
// 在这里需要初始化 context
requestQueue = Volley.newRequestQueue(context.getApplicationContext());
return requestQueue;
}
public <T> void addToRequestQueue(Request<T> request){
getRequestQueue().add(request);
}
}
在上述代码中,有一行需要初始化 context
。您可以在 ScreenOne
类的 onCreate
方法中初始化它,如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_one);
// 初始化 context
NetworkCalls.getInstance().init(this);
username = (EditText) findViewById(R.id.etUsername);
password = (EditText) findViewById(R.id.etPassword);
save = (Button) findViewById(R.id.bSave);
}
添加 init
方法到 NetworkCalls
类:
public void init(Context context) {
this.context = context;
}
这样,您应该解决了 "NetworkCalls" 的问题,应用不会崩溃,并且应该能够执行保存操作。
英文:
i'm beginner android developer. But i have problem add data from android studio to phpmyadmin. I found a ready code, but for some reason when i click on the button, the application crashes. i think the problem is with "NetworkCalls".<IF I DELETE IT THEN THE APP DOESN'T CRASH BUT DOESN'T PERFORM THE FUNCTION.
Activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.test.ScreenOne">
<EditText
android:id="@+id/etUsername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
android:layout_centerHorizontal="true"
android:layout_marginTop="330dp"/>
<EditText
android:id="@+id/etPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
android:layout_centerHorizontal="true"
android:layout_marginTop="400dp"/>
<Button
android:id="@+id/bSave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"
android:layout_centerHorizontal="true"
android:layout_marginTop="600dp"
android:onClick="saveb"/>
</RelativeLayout>
MainCode:
package com.test.test;
import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;
public class ScreenOne extends AppCompatActivity {
private static final String URL_SAVE = "http://IP/save.php";
private EditText username;
private EditText password;
Button save;
String name;
String pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_one);
username = (EditText) findViewById(R.id.etUsername);
password = (EditText) findViewById(R.id.etPassword);
save = (Button) findViewById(R.id.bSave);
}
public void saveb(View view) {
saveRequest();
}
private void saveRequest() {
name = username.getText().toString().trim();
pass = password.getText().toString().trim();
final ProgressDialog mDialog = new ProgressDialog(this);
mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
mDialog.setMessage("Loading...");
mDialog.show();
StringRequest request = new StringRequest(Request.Method.POST, URL_SAVE,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
mDialog.dismiss();
Toast.makeText(ScreenOne.this, response, Toast.LENGTH_LONG).show();
username.setText("");
password.setText("");
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mDialog.dismiss();
Toast.makeText(ScreenOne.this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> key = new HashMap<>();
key.put("username", name);
key.put("password", pass);
return key;
}
};
NetworkCalls.getInstance().addToRequestQueue(request);
}
}
NetworkCalls:
package com.test.test;
import android.content.Context;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
public class NetworkCalls {
private RequestQueue requestQueue;
private static Context context;
private static NetworkCalls ourInstance = new NetworkCalls();
public static NetworkCalls getInstance() {
return ourInstance;
}
private NetworkCalls() {
}
public RequestQueue getRequestQueue(){
requestQueue = Volley.newRequestQueue(context.getApplicationContext());
return requestQueue;
}
public <T> void addToRequestQueue(Request<T> request){
getRequestQueue().add(request);
}
}
答案1
得分: 0
以下是翻译好的部分:
"Your main problem is to pass context When the network is called NetworkCalls class got null context. so your app crushed. So change your network class like below."
"更改您的Network类如下:"
"public class NetworkCalls {
private static NetworkCalls instance;
private RequestQueue requestQueue;
private static Context mContext;
private NetworkCalls(Context context){
mContext = context;
requestQueue = getRequestQueue();
}
public static synchronized NetworkCalls getInstance(Context context){
if(instance == null){
instance = new NetworkCalls(context);
}
return instance;
}
public RequestQueue getRequestQueue(){
requestQueue = Volley.newRequestQueue(mContext.getApplicationContext());
return requestQueue;
}
public <T> void addToRequestQueue(Request<T> request){
getRequestQueue().add(request);
}
}"
"And from MainActivity saveRequest()
method change this line NetworkCalls.getInstance().addToRequestQueue(request);
to NetworkCalls.getInstance(getApplicationContext()).addToRequestQueue(request);
"
"并且在MainActivity的saveRequest()
方法中,将这一行 NetworkCalls.getInstance().addToRequestQueue(request);
更改为 NetworkCalls.getInstance(getApplicationContext()).addToRequestQueue(request);
"
"Happy Coding!"
英文:
Your main problem is to pass context When the network is called NetworkCalls class got null context. so your app crushed. So change your network class like below.
Change your Network class like this:
public class NetworkCalls {
private static NetworkCalls instance;
private RequestQueue requestQueue;
private static Context mContext;
private NetworkCalls(Context context){
mContext = context;
requestQueue = getRequestQueue();
}
public static synchronized NetworkCalls getInstance(Context context){
if(instance == null){
instance = new NetworkCalls(context);
}
return instance;
}
public RequestQueue getRequestQueue(){
requestQueue = Volley.newRequestQueue(mContext.getApplicationContext());
return requestQueue;
}
public <T> void addToRequestQueue(Request<T> request){
getRequestQueue().add(request);
}
}
And from MainActivity saveRequest()
method change this line NetworkCalls.getInstance().addToRequestQueue(request);
to NetworkCalls.getInstance(getApplicationContext()).addToRequestQueue(request);
Happy Coding!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论