1

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)

lubismail
  • 53
  • 7
  • where is LazyAdapterMenuItem.java:63 line in your code – ρяσѕρєя K Dec 18 '12 at 17:52
  • line 63 on LazyAdapterMenuItem is textCost.setText(MenuItem.get("MaxPrice")); – lubismail Dec 18 '12 at 18:07
  • means you are not putting MaxPrice in HashMap in async task. plz edit your async task code with question – ρяσѕρєя K Dec 18 '12 at 18:10
  • Isn't it this way ?? the values are obtained from jsonarray in a loop map.put("ItemCode",ItemCode[i]); map.put("ItemName", menuItemName[i]); map.put("Image_Path", menuItemStrings[i]); map.put("MaxPrice",MaxPrice[i]); MenuItem.add(map); – lubismail Dec 18 '12 at 18:13
  • ok just try as see result what happening replace `textCost.setText(MenuItem.get("MaxPrice"));` line with `if(MenuItem.get("MaxPrice")!=null){textCost.setText(MenuItem.get("MaxPrice"));}else{textCost.setText("NOTF");}` – ρяσѕρєя K Dec 18 '12 at 18:17
  • 1
    Out of curiosity, does `menu_category_item.xml` have a TextView with the id `menu_item_cost`? – Sam Dec 18 '12 at 18:28
  • @Sam : you always look very deeply and at right point :) – ρяσѕρєя K Dec 18 '12 at 18:35
  • On line if(MenuItem.get("MaxPrice")); it throws exception(I dunno if that is an exception) Source not found The jar file C://Premier\adt-bundle-windows\sdk\platforms\android 4.2\android.jar has no source attachment" – lubismail Dec 18 '12 at 18:37
  • @lubismail : first make sure you have a TextView with menu_item_cost it – ρяσѕρєя K Dec 18 '12 at 18:39
  • @lubismail : where is this line if(MenuItem.get("MaxPrice")); ?? in your code – ρяσѕρєя K Dec 18 '12 at 18:40
  • thank you Sam!! that was the error ..I mixed up the xml files ..I had renamed them a several times ..I even checked if those text views were there..but that particular layout was not being loaded...when you pointed it out i noticed the layout was wrong ..Thank you soooo much ρяσѕρєя K and Sam for your time and help...I was stuck on this for long..:) – lubismail Dec 18 '12 at 18:46
  • @Sam : plz post it as answer – ρяσѕρєя K Dec 18 '12 at 19:00
  • 1
    @ρяσѕρєя K, Sometimes I don't get these notifications, which is really annoying. I just posted an answer... which you just upvoted. Thanks! – Sam Dec 18 '12 at 19:02

1 Answers1

1

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.

Sam
  • 86,580
  • 20
  • 181
  • 179