質問

I am trying to make an app that retrieves a list of menu categories with the corresponding images from mysql database and display it in a listview(R.id.list). On clicking a list item ,The food items under the particular category is to be displayed ,again in a listview(R.id.listMenuItem).

The path of the image is stored in the database .I followed the following links to populate the listview with the images and texviews

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

I have used the lazloading of images using an Imageloader class and a lazyAdapter Class. I was successful in displaying the first list view and followed the same step to load the second listview in the onclick event.

However my app crashes on clicking a list item.The ID of the clicked item is being passed through the Intent(I checked that).Debugging the app tells me the " jar adt-bundle-windows\sdk\platforms\android 4.2\android.jar has no source attachment"

My Activity to load the list view is

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 is an asynctask to get the image and the data assocciated from the db and add to a hash map 'map'.In the onPostExecute i have the code to set the list adapter.

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);

        }});

}
} 

The LazyAdapterMenuItem Class is as below

 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;
    }
}

On Debugging ,the exception is thrown at getView of the LazyAdapter. As many posts suggested it is returning concertView and not null.I don't what changes to make ...

My logcat looks like this

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)

Can somebody help me...I am new to android..Is there anything i have overlooked ..Thanks in advance for your help...

(I am running Eclipse IDE on Windows 7 android 4.2)

役に立ちましたか?

解決

I am providing the solution found in the comments so this question can be officially marked as "solved". I asked:

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

Apparently it didn't. As you know findViewById() cannot locate a View that is not attached to the current Activity, you just needed a reminder.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top