我正在用女巫开发一个应用程序,我需要不断检查数据库更改
因此,我创建了一个处理程序,并在处理程序内部使用了一个函数来检查数据库更改。

它运作良好,但是在我的旧版android 6手机中,该应用在运行2-3分钟后崩溃。

有人有线索吗?

public void search4driver(){
        final Handler handler = new Handler();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                if (driver_id==0) {
    ******                checktaxiorder(Maghadir.getUserphone());
                }else if (driver_id!=0&&condition==0){
                    condition=1;
                    driverditails(driver_id);
                }else {
                }
                handler.postDelayed(this, 1000);
                // handler.removeCallbacks(Runnable runnable);
            }
        };
        handler.postDelayed(runnable, 10000);

    }


我在与********一起显示的行中收到错误

这是我的checktaxiorder函数

    public void checktaxiorder(final String phone) {
        final StringRequest request = new StringRequest(Request.Method.POST, "http://violet-app.ir/user-taxi-req-check.php",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {


                        if (response != null) {

                            try {

                                JSONArray jsonArray = new JSONArray(response);


                                for (int i = 0; i < jsonArray.length(); i++) {
                                    JSONObject c = jsonArray.getJSONObject(i);

                                    String rannandeid = c.getString("ranandeid");
                                    driver_id=Integer.parseInt(rannandeid);
                                }


                            } catch (Exception e) {
                            }
                        } else {
                            Toast.makeText(getApplicationContext().getApplicationContext(), "no data", Toast.LENGTH_SHORT).show();
                        }
                    }


                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                Map<String, String> params = new HashMap<>();
                params.put("phone", phone);
                return params;
            }
        };

   *******    Volley.newRequestQueue(getApplicationContext()).add(request);

    }



这是我的清单


android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "ir.violetapp.violet"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation('com.zarinpal:purchase:0.0.8-beta') {
        exclude group: 'com.mcxiaoke.volley', module: 'library'
    }

    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.google.android.gms:play-services-analytics:16.0.3'
    implementation 'com.google.android.gms:play-services-maps:15.0.1'
    implementation 'com.android.volley:volley:1.1.0'
    implementation 'com.android.support:support-vector-drawable:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.0-alpha7'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'com.squareup.picasso:picasso:2.5.2'
    implementation 'co.ronash.android:pushe-base:1.6.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}



这是我的目录

05-20 12:34:43.487 13609-13609/ir.violetapp.violet E/AndroidRuntime: FATAL EXCEPTION: main
    Process: ir.violetapp.violet, PID: 13609
    java.lang.OutOfMemoryError: pthread_create (1040KB stack) failed: Try again
        at java.lang.Thread.nativeCreate(Native Method)
        at java.lang.Thread.start(Thread.java:1063)
        at com.android.volley.RequestQueue.start(RequestQueue.java:135)
        at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:91)
        at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:67)
        at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:102)
        at ir.violetapp.violet.Taxifind.checktaxiorder(Taxifind.java:161)
        at ir.violetapp.violet.Taxifind$4.run(Taxifind.java:102)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:158)
        at android.app.ActivityThread.main(ActivityThread.java:7225)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

最佳答案

问题是我正在为每个请求创建一个新的RequestQueue。
那就是原因。解决方案:
私有RequestQueue requestQueue;

public void uploadData(String s) {
    if (requestQueue == null) {
        requestQueue = Volley.newRequestQueue(context);
    }
}

07-27 15:30