blist_activity.java
package com.hugelview.user.demo_architecture;
import android.app.ListActivity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.hugelview.demo_architecture.HttpManager.com.HttpManager;
import java.util.ArrayList;
import java.util.List;
import demo_architecture.adapter.bookingstatus;
public class blist extends ListActivity {
ListView output;
ProgressBar pb;
List<blist> task;
List<bookingstatus> status;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_blist);
task = new ArrayList<>();
// output = (ListView) findViewById(R.id.listView);
pb = (ProgressBar) findViewById(R.id.progressBar);
if (task.size() == 0) {
pb.setVisibility(View.VISIBLE);
}task.add(this);
//
// listview_array listview = new listview_array(this, R.layout.item, status);
// setListAdapter(listview);
if (isonline()) {
Requestdata("http://cita.co.in/androidapp/vehicle_info.php");
} else {
Toast.makeText(this, "CONNECT TO INTERNET", Toast.LENGTH_LONG).show();
}
}
private void Requestdata(String uri) {
mytask task = new mytask();
task.execute(uri);
}
public boolean isonline(){
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netinfo = cm.getActiveNetworkInfo();
if (netinfo != null && netinfo.isConnectedOrConnecting()) {
return true;
}
else{
return false;
}
}
private class mytask extends AsyncTask<String, String, String>
{
@Override
protected void onPreExecute() {
task.remove(this);
if (task.size() == 0) {
pb.setVisibility(View.INVISIBLE);
}
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String content = HttpManager.getdata(params[0]);
return content;
}
@Override
protected void onPostExecute(String result) {
task.remove(this);
if (task.size() == 0) {
pb.setVisibility(View.INVISIBLE);
}
updatedisplay(result);
}
}
private void updatedisplay(String result) {
listview_array listview = new listview_array(this ,R.layout.item, status);
setListAdapter(listview);
}
// @Override
// public boolean onCreateOptionsMenu(Menu menu) {
// // Inflate the menu; this adds items to the action bar if it is present.
// getMenuInflater().inflate(R.menu.menu_blist, menu);
// return true;
// }
//
// @Override
// public boolean onOptionsItemSelected(MenuItem item) {
// // Handle action bar item clicks here. The action bar will
// // automatically handle clicks on the Home/Up button, so long
// // as you specify a parent activity in AndroidManifest.xml.
// int id = item.getItemId();
//
// //noinspection SimplifiableIfStatement
// if (id == R.id.action_settings) {
// return true;
// }
//
// return super.onOptionsItemSelected(item);
// }
}
bookingstatus.java
package demo_architecture.adapter;
/**
* Created by user on 3/2/2015.
*/
public class bookingstatus {
private int booking_id;
private String pick_up;
private String drop;
private String time;
private String date;
public int getBooking_id() {
return booking_id;
}
public void setBooking_id(int booking_id) {
this.booking_id = booking_id;
}
public String getPick_up() {
return pick_up;
}
public void setPick_up(String pick_up) {
this.pick_up = pick_up;
}
public String getDrop() {
return drop;
}
public void setDrop(String drop) {
this.drop = drop;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getDriver_mobile() {
return driver_mobile;
}
public void setDriver_mobile(String driver_mobile) {
this.driver_mobile = driver_mobile;
}
public String getDriver_name() {
return driver_name;
}
public void setDriver_name(String driver_name) {
this.driver_name = driver_name;
}
public String getVehicle_number() {
return vehicle_number;
}
public void setVehicle_number(String vehicle_number) {
this.vehicle_number = vehicle_number;
}
private String driver_name;
private String driver_mobile;
private String vehicle_number;
}
Jsonlistparser.java
package com.hugelview.demo_architecture.parser;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import demo_architecture.adapter.bookingstatus;
/**
* Created by user on 3/2/2015.
*/
public class Jsonlistparser {
public static List<bookingstatus> parsefeed(String content){
try {
JSONArray jr = new JSONArray(content);
List<bookingstatus> status =new ArrayList<>();
for (int i=0; i < jr.length(); i++){
JSONObject obj = jr.getJSONObject(i);
bookingstatus bs = new bookingstatus();
bs.setBooking_id(obj.getInt("booking id"));
bs.setDate(obj.getString("date"));
bs.setDriver_mobile(obj.getString("driver mobile"));
bs.setDriver_name(obj.getString("driver name"));
bs.setDrop(obj.getString("drop point"));
bs.setDate(obj.getString("date"));
bs.setPick_up(obj.getString("pick up"));
bs.setTime(obj.getString("time"));
bs.setVehicle_number(obj.getString("vehicle no:"));
status.add(bs);
}
return status;
} catch (JSONException e) {
e.printStackTrace();
return null;
}
}
}
activity_blist.xml
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.hugelview.user.demo_architecture.blist">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@android:id/listView" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progressBar"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:layout_below="@+id/textView"
android:id="@+id/textView1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:layout_below="@+id/textView1"
android:id="@+id/textView2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:layout_below="@+id/textView2"
android:id="@+id/textView3"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp" />
</RelativeLayout>
listview_array
package com.hugelview.user.demo_architecture;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
import demo_architecture.adapter.bookingstatus;
/**
* Created by user on 3/2/2015.
*/
public class listview_array extends ArrayAdapter<bookingstatus> {
private Context context;
private List<bookingstatus> status;
public listview_array(Context context, int resource , List<bookingstatus> objects)
{
super(context, resource, objects);
this.context =context;
this.status =objects;
}
@Override
public View getView(int position, View convertview, ViewGroup parent){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item, parent, false);
//display name in textview
bookingstatus booking = status.get(position);
TextView tv = (TextView) view.findViewById(R.id.textView);
tv.setText(booking.getPick_up());
return view;
}
}
当我在mainactivity.java的选项菜单中选择状态时,他们不应该进入列表视图,因为它应该是blist_activity.java,并且应该在开始活动时显示listview中的内容。
不幸的是我收到了错误
java.lang.NullPointerException
at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
at android.widget.ListView.setAdapter(ListView.java:487)
at android.app.ListActivity.setListAdapter(ListActivity.java:265)
at com.hugelview.user.demo_architecture.blist.updatedisplay(blist.java:112)
at com.hugelview.user.demo_architecture.blist.access$100(blist.java:22)
at com.hugelview.user.demo_architecture.blist$mytask.onPostExecute(blist.java:102)
at com.hugelview.user.demo_architecture.blist$mytask.onPostExecute(blist.java:77)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5292)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
at dalvik.system.NativeStart.main(Native Method)
请真正提出建议
答案 0 :(得分:0)
ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
在xml文件中必须提到listView的ID。试试吧。
答案 1 :(得分:0)
<强> chnage 强>
android:id="@+id/listView"
以强>
android:id="@id/android:listView"
答案 2 :(得分:0)
现在在您的xml中,
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView" /> /* <--------- */
必须如此,请尝试复制粘贴此内容并且您不太可能收到错误:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@android:id/list" />