duan7264 2016-11-26 17:00
浏览 87

Android Studio试图发送数据并使用volley检索json数组给出错误

Im trying to do a filter using mysql BETWEEN statement, so im sending start and end date to my php files and getting a json array result to be placed into ListView. However im getting this error. Pls help. Tqvm in advance.

test.java

public class test extends AppCompatActivity {
private ListView listViewManagerViewReport;

String receiveSpStartDate;
String receiveSpEndDate;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    receiveDate();
    listViewManagerViewReport = (ListView)findViewById(R.id.lvManagerViewReportResult);
    sendRequest();
}

public void receiveDate(){
    SharedPreferences preferences = getSharedPreferences("PassingDetails", MODE_PRIVATE);
    receiveSpStartDate = preferences.getString("startDate", "..");
    receiveSpEndDate = preferences.getString("endDate", "..");
}

class CustomListManagerViewReportAdapter extends ArrayAdapter<String> {
    private Activity context;
    private String[] id;

    private String[] name;
    private String[] total;
    private String[] transactionDate;
    private String[] transactionTime;
    private String[] transactionType;

    public CustomListManagerViewReportAdapter(Activity context,String[] id, String[] name, String[] total, String[] transactionDate, String[] transactionTime, String[] transactionType)
    {
        super(context, R.layout.manager_view_report_result_layout, id);

        this.context = context;
        this.id = id;

        this.name = name;
        this.total = total;
        this.transactionDate = transactionDate;
        this.transactionTime = transactionTime;
        this.transactionType = transactionType;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View listViewMVRItem = inflater.inflate(R.layout.manager_view_report_result_layout, null, true);

        TextView textViewid = (TextView)listViewMVRItem.findViewById(R.id.tvMVRid);

        TextView textViewName = (TextView)listViewMVRItem.findViewById(R.id.tvMVRTransactionInventoryName);
        TextView textViewTotal = (TextView)listViewMVRItem.findViewById(R.id.tvMVRTransactionInventoryTotal);
        TextView textViewTransactionDate = (TextView)listViewMVRItem.findViewById(R.id.tvMVRTransactionInventoryDate);
        TextView textViewTransactionTime = (TextView)listViewMVRItem.findViewById(R.id.tvMVRTransactionInventoryTime);
        TextView textViewTransactionType = (TextView)listViewMVRItem.findViewById(R.id.tvMVRTransactionInventoryType);

        textViewid.setText(id[position]);
        textViewName.setText(name[position]);
        textViewTotal.setText("TOTAL : " +total[position]);
        textViewTransactionDate.setText("DATE : " +transactionDate[position]);
        textViewTransactionTime.setText("TIME : " +transactionTime[position]);
        textViewTransactionType.setText("TYPE : " +transactionType[position]);

        return listViewMVRItem;
    }
}


private void sendRequest(){

    String url = ManagerViewReportJsonWorker.ViewReport_URL;

    final String startDate = receiveSpStartDate;
    final String endDate = receiveSpEndDate;

    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    showJSON(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(test.this,error.toString(), Toast.LENGTH_LONG).show();
                }
            }){
        @Override
        protected Map<String,String> getParams(){
            Map<String,String> params = new HashMap<String, String>();
            params.put(ManagerViewReportJsonWorker.KEY_STARTDATE,startDate);
            params.put(ManagerViewReportJsonWorker.KEY_ENDDATE,endDate);
            return params;
        }

    };

    RequestQueue requestQueue = Volley.newRequestQueue(this);
    requestQueue.add(stringRequest);

}

private void showJSON(String json) {
    ManagerViewReportJsonWorker pj = new ManagerViewReportJsonWorker(json);
    pj.parseJSON();
    CustomListManagerViewReportAdapter cl = new CustomListManagerViewReportAdapter(this,
            ManagerViewAllInventoryJsonWorker.id,
            ManagerViewReportJsonWorker.name,
            ManagerViewReportJsonWorker.total,
            ManagerViewReportJsonWorker.transactionDate,
            ManagerViewReportJsonWorker.transactionTime,
            ManagerViewReportJsonWorker.transactionType);
    listViewManagerViewReport.setAdapter(cl);

}
}

php file

<?php

$con = mysqli_connect(HOST,USER,PASS,DB);

if($_SERVER['REQUEST_METHOD']=='POST'){

$startDate = $_POST['poststartdate'];
$endDate = $_POST['postenddate'];


$sql = "SELECT * FROM inventorytransaction WHERE transactiondate BETWEEN $startDate AND '$endDate'";

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

$result = array();

while($row = mysqli_fetch_array($res))
{
array_push($result,array(
        'id'=>$row[0],
        'name'=>$row[1],
        'total'=>$row[2],
        'transactiondate'=>$row[3],
        'transactiontime'=>$row[4],
        'transactiontype'=>$row[5]));
}

echo json_encode(array("result"=>$result));

mysqli_close($con);

} 
?>

error log

11-26 16:50:23.060 7800-7800/com.example.user.inventorymanagement E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: com.example.user.inventorymanagement, PID: 7800
                                                                                java.lang.NullPointerException: storage == null
                                                                                    at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
                                                                                    at java.util.Arrays.asList(Arrays.java:155)
                                                                                    at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:137)
                                                                                    at com.example.user.inventorymanagement.test$CustomListManagerViewReportAdapter.<init>(test.java:0)
                                                                                    at com.example.user.inventorymanagement.test.showJSON(test.java:134)
                                                                                    at com.example.user.inventorymanagement.test.access$000(test.java:25)
                                                                                    at com.example.user.inventorymanagement.test$1.onResponse(test.java:107)
                                                                                    at com.example.user.inventorymanagement.test$1.onResponse(test.java:104)
                                                                                    at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)
                                                                                    at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30)
                                                                                    at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
                                                                                    at android.os.Handler.handleCallback(Handler.java:739)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                    at android.os.Looper.loop(Looper.java:148)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  • 写回答

1条回答 默认 最新

  • dongxuan58311366668 2016-11-26 17:18
    关注

    This error occurs when one of your adapter's arrays is null. Check if they are non-null and only then assign them to the adapter.

    class CustomListManagerViewReportAdapter extends ArrayAdapter<String> {
    private Activity context;
    private String[] id;
    
    private String[] name = new String[] {""};
    private String[] total = new String[] {""};
    private String[] transactionDate = new String[] {""};
    private String[] transactionTime = new String[] {""};
    private String[] transactionType = new String[] {""};
    
    public CustomListManagerViewReportAdapter(Activity context,String[] id, String[] name, String[] total, String[] transactionDate, String[] transactionTime, String[] transactionType)
    {
        super(context, R.layout.manager_view_report_result_layout, id);
    
        this.context = context;
        this.id = id;
    
        if(name != null)
            this.name = name;
        if(total != null)
            this.total = total;
        if(transactionDate != null)
            this.transactionDate = transactionDate;
        if(transactionTime != null)
            this.transactionTime = transactionTime;
        if(transactionType != null)
            this.transactionType = transactionType;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题2
  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备