美文网首页
月笔记(2018-3)

月笔记(2018-3)

作者: 寒冬_腊月 | 来源:发表于2018-03-10 15:20 被阅读11次

    解决android.view.View android.view.ViewStub.inflate()' on a null object reference异常

    解决:就是ViewStub只能调用inflate一次,代码如下:

     View viewStub = findViewById(R.id.grey_mask_container);
     viewStub.inflate();
    

    修改为

     View viewStub = findViewById(R.id.grey_mask_container);
     if(viewStub != null){
          viewStub.inflate();
     }
    

    服务器接收的Json数据有问题

    错误写法

     try{
    
                JSONArray jsonArray = new JSONArray();
    
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("goodsId", "123456");
    
                jsonArray.put(jsonObject);
    
                JSONObject paramsObj = new JSONObject();
                paramsObj.put("goods", jsonArray.toString());
    
                Log.d(TAG, "result: "+paramsObj);
            }catch (Exception e){
                e.printStackTrace();
            }
    

    输出:

    result:{"goods":"[{\"goodsId\":\"123456\"}]"}
    

    正确写法

    try{
    
                JSONArray jsonArray = new JSONArray();
    
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("goodsId", "123456");
    
                jsonArray.put(jsonObject);
    
                JSONObject paramsObj = new JSONObject();
                paramsObj.put("goods", jsonArray);
    
                Log.d(TAG, "setupView: "+paramsObj);
            }catch (Exception e){
                e.printStackTrace();
            }
    

    输出:

    result:{"goods":[{"goodsId":"123456"}]}
    

    结论:JSONArray调用了toString会导致服务器接收的数据有""

    相关文章

      网友评论

          本文标题:月笔记(2018-3)

          本文链接:https://www.haomeiwen.com/subject/sffbfftx.html