为什么获取空数据时我的应用程序崩溃?

时间:2018-07-16 12:58:40

标签: java php android arrays json

我的应用程序从PHP文件获取数据。 3个正常的textdata和5个图像url。该应用程序在带有cardviews的recyclerview中显示它们。

我的问题是,当获取空图片网址时,应用程序崩溃。

但是如果文本数据为空,则该应用程序可以正常运行。

赞:

具有内容的文本数据+具有内容的图片网址=作品

空的文本数据+空的图片网址=应用程序崩溃

空的文本数据+带有内容的图片网址=作品

包含内容+图片网址为空的文本数据=应用程序崩溃

这是获取和发送(到RecyclerviewAdapter)代码:

public void JSON_DATA_WEB_CALL (String url){

        jsonArrayRequest = new JsonArrayRequest(url,

                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {


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

                    }
                });

        requestQueue = Volley.newRequestQueue(this);

        requestQueue.add(jsonArrayRequest);
    }

 public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array) {

    for (int i = 0; i < array.length(); i++) {

        GetDataAdapter GetDataAdapter2 = new GetDataAdapter();

        JSONObject json = null;

        try {



            json = array.getJSONObject(i);



                GetDataAdapter2.setImageUrl(json.getString(Image_URL_JSON));
                GetDataAdapter2.setImageUrl2(json.getString(Image_URL_JSON2));
                GetDataAdapter2.setImageUrl3(json.getString(Image_URL_JSON3));
                GetDataAdapter2.setImageUrl4(json.getString(Image_URL_JSON4));
                GetDataAdapter2.setImageUrl5(json.getString(Image_URL_JSON5));
                GetDataAdapter2.setPhone_number(json.getString(SOMETEXT));
                GetDataAdapter2.setName(json.getString(SOMETEXT2));

                GetDataAdapter2.setSubject2(json.getString(SOMETEXT3));

        } catch (JSONException e) {

            e.printStackTrace();
        }
        GetDataAdapter1.add(GetDataAdapter2);
    }
    recyclerViewadapter = new RecyclerViewAdapter(solutionTemplate.this, GetDataAdapter1, exerciseAdapter);

    recyclerView.setAdapter(recyclerViewadapter);
}

主要问题是:

      json = array.getJSONObject(i);

            GetDataAdapter2.setImageUrl(json.getString(Image_URL_JSON));
            GetDataAdapter2.setImageUrl2(json.getString(Image_URL_JSON2));
            GetDataAdapter2.setImageUrl3(json.getString(Image_URL_JSON3));
            GetDataAdapter2.setImageUrl4(json.getString(Image_URL_JSON4));
            GetDataAdapter2.setImageUrl5(json.getString(Image_URL_JSON5));
            GetDataAdapter2.setPhone_number(json.getString(SOMETEXT));
            GetDataAdapter2.setName(json.getString(SOMETEXT2));

            GetDataAdapter2.setSubject2(json.getString(SOMETEXT3));

工作:

json = array.getJSONObject(i);

            GetDataAdapter2.setImageUrl(json.getString(Image_URL_JSON));
            GetDataAdapter2.setImageUrl2(json.getString(Image_URL_JSON2));
            GetDataAdapter2.setImageUrl3(json.getString(Image_URL_JSON3));
            GetDataAdapter2.setImageUrl4(json.getString(Image_URL_JSON4));
            GetDataAdapter2.setImageUrl5(json.getString(Image_URL_JSON5));

不起作用:

           GetDataAdapter2.setPhone_number(json.getString(SOMETEXT));
            GetDataAdapter2.setName(json.getString(SOMETEXT2));
            GetDataAdapter2.setSubject2(json.getString(SOMETEXT3));

获取包含内容的图片网址是必要的!

但是没有它应该可以工作!

这是GetDataAdapter:

public String getPhone_number() {

    return phone_number;
}

public void setPhone_number(String phone_number1) {

    this.phone_number = phone_number1;
}

public String getSubject2() {

    return subject2;
}

public void setSubject2(String subject2) {

    this.subject2 = subject2;
}
public String getImageUrl() {

    return ImageURL;
}
public void setImageUrl(String imageServerUrl) {

    this.ImageURL = imageServerUrl;
}



public String getImageUrl2() {

    return ImageURL2;
}

public void setImageUrl2(String imageServerUrl2) {

    this.ImageURL2 = imageServerUrl2;
}



public String getImageUrl3() {

    return ImageURL3;
}

public void setImageUrl3(String imageServerUrl3) {

    this.ImageURL3 = imageServerUrl3;
}



public String getImageUrl4() {

    return ImageURL4;
}

public void setImageUrl4(String imageServerUrl4) {

    this.ImageURL4 = imageServerUrl4;
}



public String getImageUrl5() {

    return ImageURL5;
}

public void setImageUrl5(String imageServerUrl5) {

    this.ImageURL5 = imageServerUrl5;
}

RecyclerViewAdapter:

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
    if (holder instanceof ViewHolder) {

        final GetDataAdapter current = getItem(position - 1);
        final ViewHolder holder1 = (ViewHolder) holder;
        final ImageLoader imageLoader = ImageAdapter.getInstance(context).getImageLoader();
        final ImageLoader imageLoader2 = ImageAdapter.getInstance(context).getImageLoader();
        final ImageLoader imageLoader3 = ImageAdapter.getInstance(context).getImageLoader();
        final ImageLoader imageLoader4 = ImageAdapter.getInstance(context).getImageLoader();
        final ImageLoader imageLoader5 = ImageAdapter.getInstance(context).getImageLoader();

       imageLoader.get(current.getImageUrl(),
                ImageLoader.getImageListener(
                        holder1.imageView1,
                        R.mipmap.ic_launcher_image,
                        android.R.drawable.ic_dialog_alert
                )
        );
        imageLoader2.get(current.getImageUrl2(),
                ImageLoader.getImageListener(
                        holder1.imageView2,
                        R.mipmap.ic_launcher_image,
                        android.R.drawable.ic_dialog_alert
                )
        );
        imageLoader3.get(current.getImageUrl3(),
                ImageLoader.getImageListener(
                        holder1.imageView3,
                        R.mipmap.ic_launcher_image,
                        android.R.drawable.ic_dialog_alert
                )
        );
        imageLoader4.get(current.getImageUrl4(),
                ImageLoader.getImageListener(
                        holder1.imageView4,
                        R.mipmap.ic_launcher_image,
                        android.R.drawable.ic_dialog_alert
                )
        );
        imageLoader5.get(current.getImageUrl5(),
                ImageLoader.getImageListener(
                        holder1.imageView5,
                        R.mipmap.ic_launcher_image,
                        android.R.drawable.ic_dialog_alert
                )
        );


    holder1.imageView1.setImageUrl(current.getImageUrl(), imageLoader);
    holder1.imageView2.setImageUrl(current.getImageUrl2(), imageLoader2);
    holder1.imageView3.setImageUrl(current.getImageUrl3(), imageLoader3);
    holder1.imageView4.setImageUrl(current.getImageUrl4(), imageLoader4);
    holder1.imageView5.setImageUrl(current.getImageUrl5(), imageLoader5);
    holder1.NameTextView.setText(current.getName());
    holder1.PhoneNumberTextView.setText(current.getPhone_number());
    holder1.SubjectTextView.setText(current.getSubject2());

ImageAdapter:

public class ImageAdapter {

public static ImageAdapter imageAdapter;

public Network networkOBJ ;

public RequestQueue requestQueue1;

public ImageLoader Imageloader1;

public Cache cache1 ;

public static Context context1;

LruCache<String, Bitmap> LRUCACHE = new LruCache<String, Bitmap>(30);

private ImageAdapter(Context context) {

    this.context1 = context;

    this.requestQueue1 = RequestQueueFunction();

    Imageloader1 = new ImageLoader(requestQueue1, new ImageLoader.ImageCache() {

        @Override
        public Bitmap getBitmap(String URL) {

            return LRUCACHE.get(URL);
        }

        @Override
        public void putBitmap(String url, Bitmap bitmap) {

            LRUCACHE.put(url, bitmap);
        }
    });
}

public ImageLoader getImageLoader() {

    return Imageloader1;
}

public static ImageAdapter getInstance(Context SynchronizedContext) {

    if (imageAdapter == null) {

        imageAdapter = new ImageAdapter(SynchronizedContext);
    }
    return imageAdapter;
}

public RequestQueue RequestQueueFunction() {

    if (requestQueue1 == null) {

        cache1 = new DiskBasedCache(context1.getCacheDir());

        networkOBJ = new BasicNetwork(new HurlStack());

        requestQueue1 = new RequestQueue(cache1, networkOBJ);

        requestQueue1.start();
    }
    return requestQueue1;
}

}

PHP:

<?php




 $conn = mysqli_connect('localhost', 'root', '','webapp');
 $id = $_GET['id'];


$sql = "SELECT * FROM answers WHERE id='$id' ORDER BY id DESC";

$res = mysqli_query($conn,$sql);

$result = array();



while($row = mysqli_fetch_array($res)){
    array_push($result,array(
        'url2'=>$row['images'],
        'url3'=>$row['images2'],
        'url4'=>$row['images3'],
        'url5'=>$row['images4'],
        'url6'=>$row['images5'],
        'subject'=>$row['subject'],
        'phone'=>$row['phone'],
        'name'=>$row['name']));
}  

echo json_encode(array_values($result));



mysqli_close($conn);


      ?>

如果PHP发送的url2, url3, url4...内容不正确,则应用程序会崩溃,但是phone, name, subject的值为空则不会出现问题。

我认为Android在转换和显示空图片网址方面存在问题。

编辑:

很多用户说我必须添加Logcat:

    07-16 13:12:10.713 1578-1990/system_process W/ActivityManager:   Force finishing activity com.example.jakob.webapplicationsave/.solutionTemplate
    07-16 13:12:10.822 1578-1990/system_process W/ActivityManager:   Force finishing activity com.example.jakob.webapplicationsave/.Main2Activity
    07-16 13:12:10.946 1578-2630/system_process I/OpenGLRenderer: Initialized EGL, version 1.4
    07-16 13:12:10.947 1578-2630/system_process D/EGL_emulation: eglCreateContext: 0x9c2be040: maj 2 min 0 rcv 2
    07-16 13:12:10.953 1578-2630/system_process D/EGL_emulation: eglMakeCurrent: 0x9c2be040: ver 2 0 (tinfo 0x9efbd6e0)
    07-16 13:12:10.962 1578-2630/system_process D/EGL_emulation: eglMakeCurrent: 0x9c2be040: ver 2 0 (tinfo 0x9efbd6e0)
    07-16 13:12:11.323 1578-1592/system_process W/ActivityManager: Activity pause timeout for ActivityRecord{e46f507 u0 com.example.jakob.webapplicationsave/.solutionTemplate t497 f}
    07-16 13:12:11.372 1930-2076/com.android.launcher3 I/OpenGLRenderer: Initialized EGL, version 1.4
    07-16 13:12:11.374 1930-2076/com.android.launcher3 D/EGL_emulation: eglCreateContext: 0xae414660: maj 2 min 0 rcv 2
    07-16 13:12:11.385 1930-2076/com.android.launcher3 D/EGL_emulation: eglMakeCurrent: 0xae414660: ver 2 0 (tinfo 0xae412440)
    07-16 13:12:11.423 1930-2076/com.android.launcher3 D/EGL_emulation: eglMakeCurrent: 0xae414660: ver 2 0 (tinfo 0xae412440)
    07-16 13:12:13.537 1270-1616/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 38006369 , only wrote 37870012
    07-16 13:12:14.795 13519-13519/com.example.jakob.webapplicationsave I/Process: Sending signal. PID: 13519 SIG: 9
    07-16 13:12:14.814 1578-2630/system_process D/EGL_emulation: eglMakeCurrent: 0x9c2be040: ver 2 0 (tinfo 0x9efbd6e0)
    07-16 13:12:14.815 1578-2630/system_process E/Surface: getSlotFromBufferLocked: unknown buffer: 0x9b6834a0
    07-16 13:12:14.830 1578-2182/system_process E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!  (parcel size = 104)
    07-16 13:12:14.830 1578-2182/system_process W/InputMethodManagerService: Got RemoteException sending setActive(false) notification to pid 13519 uid 10062
    07-16 13:12:14.833 1578-2182/system_process E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!  (parcel size = 104)
    07-16 13:12:14.847 1578-1713/system_process D/GraphicsStats: Buffer count: 5
    07-16 13:12:14.847 1578-1625/system_process W/InputDispatcher: channel '7af27b1 com.example.jakob.webapplicationsave/com.example.jakob.webapplicationsave.Main2Activity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
    07-16 13:12:14.847 1578-2182/system_process I/WindowState: WIN DEATH: Window{7af27b1 u0 com.example.jakob.webapplicationsave/com.example.jakob.webapplicationsave.Main2Activity}
    07-16 13:12:14.847 1578-1625/system_process E/InputDispatcher: channel '7af27b1 com.example.jakob.webapplicationsave/com.example.jakob.webapplicationsave.Main2Activity (server)' ~ Channel is unrecoverably broken and will be disposed!
    07-16 13:12:14.847 1578-1625/system_process W/InputDispatcher: channel 'cd3daa3 com.example.jakob.webapplicationsave/com.example.jakob.webapplicationsave.solutionTemplate (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
    07-16 13:12:14.847 1578-1625/system_process E/InputDispatcher: channel 'cd3daa3 com.example.jakob.webapplicationsave/com.example.jakob.webapplicationsave.solutionTemplate (server)' ~ Channel is unrecoverably broken and will be disposed!
    07-16 13:12:14.847 1578-2182/system_process W/InputDispatcher: Attempted to unregister already unregistered input channel '7af27b1 com.example.jakob.webapplicationsave/com.example.jakob.webapplicationsave.Main2Activity (server)'
    07-16 13:12:14.848 1578-2179/system_process I/WindowState: WIN DEATH: Window{cd3daa3 u0 com.example.jakob.webapplicationsave/com.example.jakob.webapplicationsave.solutionTemplate}
    07-16 13:12:14.848 1578-2179/system_process W/InputDispatcher: Attempted to unregister already unregistered input channel 'cd3daa3 com.example.jakob.webapplicationsave/com.example.jakob.webapplicationsave.solutionTemplate (server)'
    07-16 13:12:14.853 1259-1259/? W/SurfaceFlinger: couldn't log to binary event log: overflow.
    07-16 13:12:14.853 1578-1963/system_process I/ActivityManager: Process com.example.jakob.webapplicationsave (pid 13519) has died
    07-16 13:12:15.026 1578-1597/system_process W/AppOps: Finishing op nesting under-run: uid 1000 pkg android code 24 time=0 duration=0 nesting=0
    07-16 13:12:17.958 1270-1616/? W/audio_hw_generic: Not supplying enough data to HAL, expected position 38145850 , only wrote 38008822
    07-16 13:13:00.065 1259-1351/? D/hwcomposer: hw_composer sent 312 syncs in 60s
    07-16 13:14:00.056 1259-1351/? D/hwcomposer: hw_composer sent 5 syncs in 60s

0 个答案:

没有答案