我正在使用listview和基于@fedor帖子的延迟适配器构建应用link
这是我的第一个活动:
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<HashMap<String, String>> userList = new ArrayList<HashMap<String, String>>();
// 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<String, String> map = new HashMap<String, String>();
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<HashMap<String, String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapter(Activity a, ArrayList<HashMap<String, String>> 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<String, String> resto = new HashMap<String, String>();
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)
我的程序总是强行关闭...... 也许有人可以帮助我...
答案 0 :(得分:1)
你应该注意这部分错误:
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)
特别是:
MenuViewAll.java:102
这意味着该行中的某些内容(我不会指出行:))NULL
,但您尝试使用它。寻找使用可能是NULL
示例(只是一个例子,因为我不知道它是什么线)。 让我们说这句话:
JSONObject list = listresto.getJSONObject(i);
出于某种原因出错了。 (没有JSONobject
i
,listresto是假的,我不知道。然后在此:
map.put(KEY_ID, list.getString("id_resto"));
将提供nullpointer,因为您无法在getString
对象上执行NULL
。
这样: