Java 使用列表视图、JSON填充警报对话框
我试图用son响应填充一个警报对话框,无论我遇到以下错误: java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.ListView.setAdapter(android.widget.ListAdapter)” 正如其他帖子所建议的那样,我在onCreate中扩展了列表视图 我已经包括了所有相关的Java 使用列表视图、JSON填充警报对话框,java,android,json,xml,android-alertdialog,Java,Android,Json,Xml,Android Alertdialog,我试图用son响应填充一个警报对话框,无论我遇到以下错误: java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.ListView.setAdapter(android.widget.ListAdapter)” 正如其他帖子所建议的那样,我在onCreate中扩展了列表视图 我已经包括了所有相关的xml和java代码 <?xml version="1.0" encoding="utf-8"?> <L
xml
和java
代码
<?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>
公共类QueueStatusFragment扩展了片段{
ArrayList oslist=新的ArrayList();
私有文本视图txtQueue;
私有文本视图txtCust;
私有文本视图txtTime;
私有文本视图TXT分支;
私有字符串urlString;
私人按钮btnLeave;
私人智能身份证;
列表视图列表;
公共队列状态片段()
{}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图v=充气机。充气(R.layout.fragment\u队列状态,容器,错误);
txtQueue=(TextView)v.findviewbyd(R.id.txtQueue);
txtTime=(TextView)v.findViewById(R.id.txtTime);
txtCust=(TextView)v.findViewById(R.id.txtCust);
txtBranch=(TextView)v.findViewById(R.id.txtBranch);
列表=(ListView)v.findViewById(R.id.listviewResp);
btnLeave=(按钮)v.findviewbyd(R.id.btnLeave);
SessionV globalVariable=(SessionV)getActivity().getApplicationContext();
URL字符串=”http://172.20.10.5:1012/easyQ.svc/rest/queueDetails/“+globalVariable.getCustId();
新建ProcessJson().execute(urlString);
System.out.println(“工程”);
btnLeave.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
AlertDialog AlertDialog=新建AlertDialog.Builder(
getActivity()).create();
alertDialog.setTitle(“警报”);
setMessage(“您确定要离开队列吗?”);
alertDialog.setButton(“确定”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
URL字符串=”http://172.20.10.5:1012/easyQ.svc/rest/reasons";
新建getReasions().execute(urlString);
对于(int i=0;i