我正在尝试使用文本和图片创建Listview。图像从URL显示。
我从数据库中提取文本和图像URL,并将其存储在ArrayList< HashMap<字符串,字符串>>。
在我的代码下面但没有工作。
适配器出了问题,但我无法找到问题 有人能帮帮我吗? 谢谢。
活动:
public class Test extends Activity {
ListView list;
LazyAdapter adapter;
String result;
ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
list=(ListView)findViewById(R.id.list);
try{
DBConnection DBC = new DBConnection();
String URL = "http://10.0.2.2/Android.php";
DBC.DBConnection2(URL);
result = DBC.dd.getResult();
}catch (Exception e) {
Toast.makeText(getApplicationContext(), "Erreur BD access", Toast.LENGTH_LONG).show();
}
try{
JSONArray jArray = new JSONArray(result);
JSONObject json_data=null;
for(int j=0;j<jArray.length();j++){
json_data = jArray.getJSONObject(j);
String name = json_data.getString("Name");
String title = json_data.getString("Title");
String logo = json_data.getString("Logo");
map = new HashMap<String, String>();
map.put("name", name);
map.put("title", title);
map.put("img", logo);
listItem.add(map);
}
}catch (JSONException e1) {
e1.printStackTrace();
}
adapter=new LazyAdapter(this, listItem);
list.setAdapter(adapter);
}
}
适配器:
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>> list) {
activity = a;
data=list;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
public int getCount() {
return data.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.item, null);
TextView text=(TextView)vi.findViewById(R.id.text);;
ImageView image=(ImageView)vi.findViewById(R.id.image);
try {
HashMap<String, String> ma = data.get(position);
for(Entry<String, String> mapEntry: ma.entrySet()) {
String key = mapEntry.getKey();
String value = mapEntry.getValue();
if (key.equals("name")) text.setText(value);
if (key.equals("img")) imageLoader.DisplayImage(value, image);
}
}catch (Exception e){
e.printStackTrace();
}
return vi;
}
}
日志
06-03 02:34:33.292: E/AndroidRuntime(747): FATAL EXCEPTION: main
06-03 02:34:33.292: E/AndroidRuntime(747): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fedorvlasov.lazylist/com.fedorvlasov.lazylist.Test}: java.lang.NullPointerException
06-03 02:34:33.292: E/AndroidRuntime(747): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-03 02:34:33.292: E/AndroidRuntime(747): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-03 02:34:33.292: E/AndroidRuntime(747): at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-03 02:34:33.292: E/AndroidRuntime(747): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-03 02:34:33.292: E/AndroidRuntime(747): at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 02:34:33.292: E/AndroidRuntime(747): at android.os.Looper.loop(Looper.java:137)
06-03 02:34:33.292: E/AndroidRuntime(747): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-03 02:34:33.292: E/AndroidRuntime(747): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 02:34:33.292: E/AndroidRuntime(747): at java.lang.reflect.Method.invoke(Method.java:511)
06-03 02:34:33.292: E/AndroidRuntime(747): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-03 02:34:33.292: E/AndroidRuntime(747): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-03 02:34:33.292: E/AndroidRuntime(747): at dalvik.system.NativeStart.main(Native Method)
06-03 02:34:33.292: E/AndroidRuntime(747): Caused by: java.lang.NullPointerException
06-03 02:34:33.292: E/AndroidRuntime(747): at com.fedorvlasov.lazylist.Test.onCreate(Test.java:84)
06-03 02:34:33.292: E/AndroidRuntime(747): at android.app.Activity.performCreate(Activity.java:5008)
06-03 02:34:33.292: E/AndroidRuntime(747): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-03 02:34:33.292: E/AndroidRuntime(747): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-03 02:34:33.292: E/AndroidRuntime(747): ... 11 more
此处指定的行:
Caused by: java.lang.NullPointerException at com.fedorvlasov.lazylist.Test.onCreate(Test.java:84)
是
list.setAdapter(adapter);
答案 0 :(得分:0)
我最好的猜测是list
布局中不存在ID为test.xml
的ListView。您可以发布test.xml
的内容吗?