使用列表视图填充警报对话框,JSON

时间:2016-09-22 11:34:24

标签: java android json xml alertdialog

我正在尝试使用儿子的响应来填充警报对话框,我怎么会收到以下错误:

  

java.lang.NullPointerException:尝试调用虚方法' void android.widget.ListView.setAdapter(android.widget.ListAdapter)'在空对象引用上

我在onCreate中夸大了列表视图,正如其他帖子所暗示的那样

我已包含所有相关的xmljava代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    <CheckedTextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="?listPreferredItemHeightSmall"
        android:checkMark="?android:attr/listChoiceIndicatorSingle"
        android:gravity="center_vertical"
        android:paddingLeft="?listPreferredItemPaddingLeft"
        android:paddingRight="?listPreferredItemPaddingRight"
        android:textAppearance="?textAppearanceListItemSmall" />

</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/colorPrimary"
    tools:context=".Queue.QueueStatusFragment">
    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listviewResp"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingTop="15dp"
        android:layout_centerVertical="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

public class QueueStatusFragment extends Fragment{

    ArrayList<HashMap<String, String>> oslist = new ArrayList<HashMap<String, String>>();

    private TextView txtQueue;
    private TextView txtCust;
    private TextView txtTime;
    private TextView txtBranch;
    private String urlString;
    private Button btnLeave;
    private int reasonId;
    ListView list;
    public QueueStatusFragment()
    {}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_queuestatus, container, false);
       txtQueue = (TextView) v.findViewById(R.id.txtQueue);
        txtTime = (TextView) v.findViewById(R.id.txtTime);
       txtCust = (TextView) v.findViewById(R.id.txtCust);
        txtBranch = (TextView) v.findViewById(R.id.txtBranch);
        list=(ListView)v.findViewById(R.id.listviewResp);
        btnLeave = (Button) v.findViewById(R.id.btnLeave);
        SessionV globalVariable = (SessionV) getActivity().getApplicationContext();


        urlString = "http://172.20.10.5:1012/easyQ.svc/rest/queueDetails/" + globalVariable.getCustId();
        new ProcessJson().execute(urlString);
        System.out.println("works");

        btnLeave.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                AlertDialog alertDialog = new AlertDialog.Builder(
                        getActivity()).create();

                alertDialog.setTitle("Alert");

                alertDialog.setMessage("You are about to leave the queue, are you sure?");


                alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        urlString = "http://172.20.10.5:1012/easyQ.svc/rest/reasons";
                        new getReasons().execute(urlString);

                        for(int i = 0; i < oslist.size();i++)
                        {
                            String id = oslist.get(i).get("id");
                            System.out.println(id);
                        }
                    }
                });
                alertDialog.show();
               // SessionV globalVariable = (SessionV) getActivity().getApplicationContext();
                //String urlString1 = "http://172.20.10.5:1012/easyQ.svc/rest/leaveQueue";
               // new JsonHandler().execute(urlString1);
                //System.out.println("works");
            }
        });

        // Inflate the layout for this fragment
        return v;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    private class ProcessJson extends AsyncTask<String, Void, String> {
        protected String doInBackground(String... strings) {
            String stream;
            String urlString = strings[0];
            HTTPDataHandler hh = new HTTPDataHandler();
            stream = hh.GetHTTPData(urlString);
            // Return the data from specified url
            System.out.println(stream);
            return stream;
        }

        protected void onPostExecute(String stream) {
            if (stream != null) {
                String[] array = stream.split(",");
                String part1 = array[1];
                String part2 = array[2];
                String part3 = array[3];
                String part4 = array[4];
                String queueId = array[5];
                SessionV globalVariable = (SessionV) getActivity().getApplicationContext();
                globalVariable.setQueueId(queueId);

                if (part1 != null && part2 != null && part3 != null) {
                    int minutes = (int) Integer.parseInt(part1) / 60;
                    txtBranch.append("Branch Name: " + part4);
                    txtQueue.append("Service Name: " + part3);
                    txtCust.append("Queue Position: " + part2);
                    txtTime.append("Waiting Time: " + minutes + " minutes");

                }
            }
        }
    }

    private class getReasons extends AsyncTask<String, Void, String>{
        protected String doInBackground(String... strings) {
            String stream;
            String urlString = strings[0];
            HTTPDataHandler hh = new HTTPDataHandler();
            stream = hh.GetHTTPData(urlString);
            // Return the data from specified url
            System.out.println(stream);
            return stream;
        }

        protected void onPostExecute(String stream) {
            if (stream != null)
            {
               try
               {
                    JSONObject object = new JSONObject(stream);
                    JSONArray array = object.getJSONArray("reasonsResult");

                   for(int i = 0 ; i < array.length(); i++) {
                       JSONObject reasonObj = array.getJSONObject(i);
                       String ID = reasonObj.getString("reason_leaving_id");
                       String reason = reasonObj.getString("description");

                       HashMap<String, String> map = new HashMap<String, String>();
                       map.put("description", reason);
                       map.put("id", ID);
                       oslist.add(map);
                   }

                   AlertDialog alertDialog = new AlertDialog.Builder(
                           getActivity()).create();

                   alertDialog.setTitle("Alert");

                   alertDialog.setMessage("You are about to leave the queue, are you sure?");
                   for(int i = 0; i < oslist.size(); i++)
                   {

                       ListAdapter adapter = new SimpleAdapter(getActivity(), oslist,
                               R.layout.dialog_list,
                               new String[] { "description"}, new int[] {
                               R.id.text1});

                       list.setAdapter(adapter);
                       list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                           @Override
                           public void onItemClick(AdapterView<?> parent, View view,
                                                   int position, long id) {
                               String branchId = oslist.get(+position).get("id");
                               final SessionV globalVariable = (SessionV) getActivity().getApplicationContext();
                               globalVariable.setBranchId(branchId);


                               Fragment fragment = null;
                               fragment = new JoinFragment();
                               getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.container_body, fragment).commit();

                           }
                       });
                   }

                   alertDialog.show();
               }
               catch(JSONException e)
               {
                   e.printStackTrace();
               }
            }
        }
    }
    private class JsonHandler extends AsyncTask<String, Void, String> {
        protected String doInBackground(String... strings) {
            String stream;
            String urlString = strings[0];
            HTTPDataHandler hh = new HTTPDataHandler();
            final SessionV globalVariable = (SessionV) getActivity().getApplicationContext();

            System.out.println("sssss" + globalVariable.getQueueId() + globalVariable.getCustId());
            JSONObject sender = new JSONObject();
            try {
                sender.put("queueid", globalVariable.getQueueId());
                sender.put("customerid", globalVariable.getCustId());
                //sender.put("reasonid",);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            stream = hh.POST(urlString, sender);
            // Return the data from specified url
            System.out.println(stream);
            return stream;
        }

        protected void onPostExecute(String stream) {
            if (stream != null) {
                if (stream.equals("\"Successful\"")) {
                    Toast.makeText(getActivity().getApplicationContext(),
                            "Successfully left Queue", Toast.LENGTH_LONG).show();
                    Fragment fragment=null;
                    fragment=new HomeFragment();
                    getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.container_body, fragment).commit();
                } else if (stream.equals("\"Not Exist\"")) {
                    Toast.makeText(getActivity().getApplicationContext(),
                            "Unable to leave Queue", Toast.LENGTH_LONG).show();
                } else if (stream.equals("\"Not exist\"")) {
                    Toast.makeText(getActivity().getApplicationContext(),
                            "Not in queue", Toast.LENGTH_LONG).show();
                }
            }
        }
    }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/colorPrimary"
    tools:context=".Queue.QueueStatusFragment" >

    <TextView
        android:id="@+id/txtBranch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="8"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="false"
        android:text="hello"
        android:layout_marginTop="100dp"
        android:gravity="center|left"
        android:textColor="#ffffff"
        android:textColorHint="#ffffff" />

    <TextView
        android:id="@+id/txtQueue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="8"
        android:text="hello1"
        android:layout_marginTop="160dp"
        android:gravity="center|left"
        android:textColor="#ffffff"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="false"
        android:textColorHint="#ffffff" />

    <TextView
        android:id="@+id/txtTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="8"
        android:text="hello2"
        android:gravity="center|left"
        android:layout_marginTop="44dp"
        android:layout_centerInParent="false"
        android:textColor="#ffffff"
        android:textColorHint="#ffffff"
        android:layout_below="@+id/txtQueue"
        android:layout_alignStart="@+id/txtQueue" />

    <TextView
        android:id="@+id/txtCust"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="8"
        android:text="hello3"
        android:gravity="center|left"
        android:textColor="#ffffff"
        android:layout_marginTop="47dp"
        android:layout_centerInParent="false"
        android:textColorHint="#ffffff"
        android:layout_below="@+id/txtTime"
        android:layout_alignStart="@+id/txtTime" />

    <Button
        android:id="@+id/btnLeave"
        android:layout_width="312dp"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:gravity="center"
        android:text="Leave Queue"
        android:textColor="#ffffff"
        android:textStyle="bold"
        android:layout_marginBottom="65dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

我认为您的问题出在fragment_queuestatus,没有Listview。您添加以前的布局。这就是为什么它没有得到listview,当你尝试设置适配器时,你在NULL对象中设置了一些东西。因此,在ListView布局中添加fragment_queuestatus。这将解决问题。