Category: Development

NoClassDefFoundError After ADT Update

I’ve just returned to an Android project after a couple of months’ pause. After updating Eclipse and ADT, my project started throwing an exception:

06-12 21:45:26.154: ERROR/AndroidRuntime(3654): java.lang.NoClassDefFoundError: com.google.gson.gson

This JAR file was clearly in my “lib” folder in the project and in my build path. After entirely too much fiddling about, cleaning, rebuilding, and googling, I discovered my problem. Apparently, ANT is now expecting these to be in the “libs” folder. After moving all of my third party JAR files from “lib” to “libs”, deleting “lib”, and restarting Eclipse, all is right the the world again!

Using simple_list_item_2 with an ArrayAdapter (Android)

Today I wanted to display static a ListView of properties and their values. It’s Friday, so I really wanted to do this with as little work as possible. Android’s free layout simple_list_item_2 sounded like what I wanted, but it wasn’t immediately clear how to handle this. There are a lot of lazy ways we can list single values, but Googling around how to use simple_list_item_2 resulted in lots of questions with no real answer.

I found Mark Assad, who parsed the system layout files into raw XML. The led to the magic answer of TwoLineListItem widget. Finding this widget allowed me to write a simple ArrayAdapter of key/value pairs.

adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_2,list){
        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            TwoLineListItem row;
            if(convertView == null){
                LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                row = (TwoLineListItem)inflater.inflate(android.R.layout.simple_list_item_2, null);
            }else{
                row = (TwoLineListItem)convertView;
            }
            BasicNameValuePair data = list.get(position);
            row.getText1().setText(data.getName());
            row.getText2().setText(data.getValue());

            return row;
        }
    };
listView.setAdapter(adapter);

Hope this saves someone else some time, as I spent an embarrassing amount of time determined to use the simple_list_item_2 layout.