我正在尝试制作一个应用程序,该应用程序从 mysql 数据库中检索具有相应图像的菜单类别列表并将其显示在列表视图(R.id.list)中。单击列表项时,特定类别下的食物项将再次显示在列表视图(R.id.listMenuItem)中。
图像的路径存储在数据库中。我按照以下链接使用图像和 texviews 填充列表视图
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/和https://github.com/thest1/LazyList
我已经使用 Imageloader 类和lazyAdapter 类对图像进行延迟加载。我成功地显示了第一个列表视图,并按照相同的步骤在 onclick 事件中加载了第二个列表视图。
但是我的应用程序在单击列表项时崩溃。单击项的 ID 正在通过 Intent 传递(我检查过)。调试应用程序告诉我“ jar adt-bundle-windows\sdk\platforms\android 4.2\android .jar 没有源附件”
我加载列表视图的活动是
public class MenuItemActivity extends Activity {
ArrayList<HashMap<String, String>> MenuItem ;
ListView listviewMenuItem;
LazyAdapterMenuItem adapter;
String menu_item_image_url ="http://10.0.2.2/Restaurant/Images/Item_Menu/";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
MenuItem = new ArrayList<HashMap<String, String>>();
setContentView(R.layout.menu_item_list); // set the Layout
Bundle extras = getIntent().getExtras();
String menuCategclickedItem="";
if(extras!=null)
{
menuCategclickedItem=extras.getString("MCat_ID");
Toast.makeText(MenuItemActivity.this,menuCategclickedItem+" hi item selected", Toast.LENGTH_LONG).show();
}
GetMenuItemImageURL objgetMenuItemImageURL = new GetMenuItemImageURL();
objgetMenuItemImageURL.execute(menuCategclickedItem);
}
GetMenuItemImageURL 是一个异步任务,用于从数据库中获取图像和数据并添加到哈希映射“map”中。在 onPostExecute 我有设置列表适配器的代码。
protected void onPostExecute(final ArrayList<HashMap<String, String>> menuItem)
{
listviewMenuItem=(ListView)findViewById(R.id.listMenuItem);
adapter=new LazyAdapterMenuItem(MenuItemActivity.this, menuItem);
listviewMenuItem.setAdapter(adapter);
listviewMenuItem.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView tv = (TextView)view.findViewById(R.id.menu_single_item_id);
String pos = tv.getText().toString();
Intent in = new Intent(MenuItemActivity.this,ViewDetailsActivity.class);
in.putExtra("ItemCode",pos);
startActivity(in);
}});
}
}
LazyAdapterMenuItem 类如下
public class LazyAdapterMenuItem extends BaseAdapter {
private Activity activity;
private ArrayList<HashMap<String,String>> data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public LazyAdapterMenuItem(Activity a, ArrayList<HashMap<String, String>> d) {
activity = a;
data=d;
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.menu_category_item, null);
TextView textID=(TextView)vi.findViewById(R.id.menu_item_id);
TextView textName=(TextView)vi.findViewById(R.id.menu_name);
TextView textCost=(TextView)vi.findViewById(R.id.menu_item_cost);
ImageView image=(ImageView)vi.findViewById(R.id.image);
HashMap<String, String> MenuItem = new HashMap<String, String>();
MenuItem= data.get(position);
textID.setText(MenuItem.get("ItemCode"));
textName.setText(MenuItem.get("ItemName"));
textCost.setText(MenuItem.get("MaxPrice"));
imageLoader.DisplayImage(MenuItem.get("Image_Path"),image);
return vi;
}
}
在调试时,LazyAdapter 的 getView 会抛出异常。正如许多帖子所暗示的那样,它返回的是 ConcertView 而不是 null。我不知道要做出什么改变......
我的 logcat 看起来像这样
E/AndroidRuntime(1641): FATAL EXCEPTION: main
E/AndroidRuntime(1641): java.lang.NullPointerException
E/AndroidRuntime(1641):at com.premier.premierrestaurant.LazyAdapterMenuItem.getView(LazyAdapterMenuItem.java:63)
E/AndroidRuntime(1641): at android.widget.AbsListView.obtainView(AbsListView.java:2159)
E/AndroidRuntime(1641): at android.widget.ListView.makeAndAddView(ListView.java:1831)
E/AndroidRuntime(1641): at android.widget.ListView.fillDown(ListView.java:674)
E/AndroidRuntime(1641): at android.widget.ListView.fillFromTop(ListView.java:735)
E/AndroidRuntime(1641): at android.widget.ListView.layoutChildren(ListView.java:1652)
E/AndroidRuntime(1641): at android.widget.AbsListView.onLayout(AbsListView.java:1994)
E/AndroidRuntime(1641): at android.view.View.layout(View.java:14003)
E/AndroidRuntime(1641): at android.view.ViewGroup.layout(ViewGroup.java:4375)
E/AndroidRuntime(1641): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
E/AndroidRuntime(1641): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
E/AndroidRuntime(1641): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
E/AndroidRuntime(1641): at android.view.View.layout(View.java:14003)
E/AndroidRuntime(1641): at android.view.ViewGroup.layout(ViewGroup.java:4375)
E/AndroidRuntime(1641): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
E/AndroidRuntime(1641): at android.view.View.layout(View.java:14003)
E/AndroidRuntime(1641): at android.view.ViewGroup.layout(ViewGroup.java:4375)
E/AndroidRuntime(1641): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663)
E/AndroidRuntime(1641): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521)
E/AndroidRuntime(1641): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
E/AndroidRuntime(1641): at android.view.View.layout(View.java:14003)
E/AndroidRuntime(1641): at android.view.ViewGroup.layout(ViewGroup.java:4375)
E/AndroidRuntime(1641): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
有人可以帮助我...我是android新手...有什么我忽略的...在此先感谢您的帮助...
(我在 Windows 7 android 4.2 上运行 Eclipse IDE)