应用程序崩溃是因为向数据库添加数据。

huangapple go评论88阅读模式
英文:

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:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    tools:context=&quot;com.test.test.ScreenOne&quot;&gt;

    &lt;EditText
        android:id=&quot;@+id/etUsername&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:ems=&quot;10&quot;
        android:inputType=&quot;textPersonName&quot;
        android:text=&quot;Name&quot;
        android:layout_centerHorizontal=&quot;true&quot;
        android:layout_marginTop=&quot;330dp&quot;/&gt;

    &lt;EditText
        android:id=&quot;@+id/etPassword&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:ems=&quot;10&quot;
        android:inputType=&quot;textPassword&quot;
        android:layout_centerHorizontal=&quot;true&quot;
        android:layout_marginTop=&quot;400dp&quot;/&gt;

    &lt;Button
        android:id=&quot;@+id/bSave&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:text=&quot;save&quot;
        android:layout_centerHorizontal=&quot;true&quot;
        android:layout_marginTop=&quot;600dp&quot;
        android:onClick=&quot;saveb&quot;/&gt;
&lt;/RelativeLayout&gt;

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 = &quot;http://IP/save.php&quot;;
    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(&quot;Loading...&quot;);
        mDialog.show();



        StringRequest request = new StringRequest(Request.Method.POST, URL_SAVE,
                new Response.Listener&lt;String&gt;() {
                    @Override
                    public void onResponse(String response) {
                        mDialog.dismiss();
                        Toast.makeText(ScreenOne.this, response, Toast.LENGTH_LONG).show();
                        username.setText(&quot;&quot;);
                        password.setText(&quot;&quot;);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        mDialog.dismiss();
                        Toast.makeText(ScreenOne.this, &quot;Something went wrong&quot;, Toast.LENGTH_LONG).show();
                    }
                }) {
            @Override
            protected Map&lt;String, String&gt; getParams() throws AuthFailureError {
                Map&lt;String, String&gt; key = new HashMap&lt;&gt;();
                key.put(&quot;username&quot;, name);
                key.put(&quot;password&quot;, 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 &lt;T&gt; void addToRequestQueue(Request&lt;T&gt; 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 &lt;T&gt; void addToRequestQueue(Request&lt;T&gt; 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 &lt;T&gt; void addToRequestQueue(Request&lt;T&gt; request){
        getRequestQueue().add(request);
    }
}

And from MainActivity saveRequest() method change this line NetworkCalls.getInstance().addToRequestQueue(request); to NetworkCalls.getInstance(getApplicationContext()).addToRequestQueue(request);

Happy Coding!

huangapple
  • 本文由 发表于 2020年7月29日 21:29:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63154729.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定