gpt4 book ai didi

android - ListView 崩溃应用程序 :Java null Pointer Exception

转载 作者:行者123 更新时间:2023-11-30 03:52:22 27 4
gpt4 key购买 nike

我正在尝试制作一个应用程序,该应用程序从 mysql 数据库中检索具有相应图像的菜单类别列表,并将其显示在 ListView (R.id.list)中。单击列表项时,将再次在 ListView (R.id.listMenuItem)中显示特定类别下的食品。

图像的路径存储在数据库中。我按照以下链接用图像和 texviews 填充 ListView

http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/https://github.com/thest1/LazyList

我使用了 Imageloader 类和 lazyAdapter 类来延迟加载图像。我成功地显示了第一个 ListView ,并按照相同的步骤在 onclick 事件中加载了第二个 ListView 。

但是我的应用程序在单击列表项时崩溃。被单击项的 ID 正在通过 Intent 传递(我检查过)。调试应用程序告诉我“jar adt-bundle-windows\sdk\platforms\android 4.2\android.jar没有源码附件”

我加载 ListView 的 Activity 是

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 是一个异步任务,用于获取图像和与数据库关联的数据,并将其添加到 HashMap “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)

最佳答案

我提供了在评论中找到的解决方案,因此这个问题可以正式标记为“已解决”。我问:

Does menu_category_item.xml have a TextView with the id menu_item_cost?

显然没有。如您所知,findViewById() 无法找到未附加到当前 Activity 的 View ,您只需要一个提醒。

关于android - ListView 崩溃应用程序 :Java null Pointer Exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13938581/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com