我在这link上使用基于@fedor发布的listview和lazy适配器构建应用程序
这是我的第一个活动:
public class MenuViewAll extends ListActivity {
// url to make request
private static String url = "http://10.0.2.2/culigui/getdataresto.php";
//public static String TAG_NAME,TAG_ADDRESS;
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_ADDRESS = "address";
static final String KEY_THUMB = "linkthumb";
ListView list;
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_view_all);
// Hashmap for ListView
ArrayList> userList = new ArrayList>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting JSONArray of listresto
JSONArray listresto = json.getJSONArray("listresto");
// looping through All listresto
for(int i = 0; i < listresto.length(); i++){
HashMap map = new HashMap();
JSONObject list = listresto.getJSONObject(i);
// insert String to Local Variable
map.put(KEY_ID, list.getString("id_resto"));
map.put(KEY_NAME, list.getString("nama_resto"));
map.put(KEY_ADDRESS, list.getString("alamat_resto"));
map.put(KEY_THUMB, list.getString("thumb_img"));
userList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
adapter = new LazyAdapter(this, userList);
list.setAdapter(adapter);
// ListView yourListView = getListView()}
这里是我的懒适配器类:
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private ArrayList> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList> d) {
activity = a;
data=d;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.listitemviewall, null);
TextView namaresto = (TextView)vi.findViewById(R.id.name); // resto name
TextView alamatresto = (TextView)vi.findViewById(R.id.address); // resto address
ImageView thumb_image=(ImageView)vi.findViewById(R.id.defaultthumb); // thumb image
HashMap resto = new HashMap();
resto = data.get(position);
// Setting all values in listview
namaresto.setText(resto.get(MenuViewAll.KEY_NAME));
alamatresto.setText(resto.get(MenuViewAll.KEY_ADDRESS));
imageLoader.DisplayImage(resto.get(MenuViewAll.KEY_THUMB), thumb_image);
return vi;
}}并在我运行我的代码后,这里是logcat:
04-05 14:51:32.714: D/dalvikvm(311): GC_EXTERNAL_ALLOC freed 869 objects / 60304 bytes in 76ms
04-05 14:51:46.235: D/dalvikvm(311): GC_EXTERNAL_ALLOC freed 757 objects / 43592 bytes in 362ms
04-05 14:51:46.945: D/AndroidRuntime(311): Shutting down VM
04-05 14:51:46.945: W/dalvikvm(311): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-05 14:51:46.975: E/AndroidRuntime(311): FATAL EXCEPTION: main
04-05 14:51:46.975: E/AndroidRuntime(311): java.lang.RuntimeException: Unable to start activity ComponentInfo{last.project.CuliGUI/last.project.CuliGUI.MenuViewAll}: java.lang.NullPointerException
04-05 14:51:46.975: E/AndroidRuntime(311): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-05 14:51:46.975: E/AndroidRuntime(311): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-05 14:51:46.975: E/AndroidRuntime(311): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-05 14:51:46.975: E/AndroidRuntime(311): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-05 14:51:46.975: E/AndroidRuntime(311): at android.os.Handler.dispatchMessage(Handler.java:99)
04-05 14:51:46.975: E/AndroidRuntime(311): at android.os.Looper.loop(Looper.java:123)
04-05 14:51:46.975: E/AndroidRuntime(311): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-05 14:51:46.975: E/AndroidRuntime(311): at java.lang.reflect.Method.invokeNative(Native Method)
04-05 14:51:46.975: E/AndroidRuntime(311): at java.lang.reflect.Method.invoke(Method.java:521)
04-05 14:51:46.975: E/AndroidRuntime(311): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-05 14:51:46.975: E/AndroidRuntime(311): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-05 14:51:46.975: E/AndroidRuntime(311): at dalvik.system.NativeStart.main(Native Method)
04-05 14:51:46.975: E/AndroidRuntime(311): Caused by: java.lang.NullPointerException
04-05 14:51:46.975: E/AndroidRuntime(311): at last.project.CuliGUI.MenuViewAll.onCreate(MenuViewAll.java:102)我的程序总是强制关闭...
也许有人可以帮助我...