Search code examples
androidxmllistviewandroid-tabhost

Android TabHost with ListView


Application closes when I click on TabHost. I use XML parser. Example take away http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/ . Here's some code for my project: AndroidTabAndListView (for TabHost)

public class AndroidTabAndListView extends TabActivity {

private static final String INBOX_SPEC = "Inbox";
private static final String OUTBOX_SPEC = "Profile";
private static final String PROFILE_SPEC = "ХЗ";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TabHost tabHost = getTabHost();

    // Inbox Tab
    TabSpec inboxSpec = tabHost.newTabSpec(INBOX_SPEC);
    // Tab Icon
    inboxSpec.setIndicator(INBOX_SPEC, getResources().getDrawable(R.drawable.arrow));
    Intent inboxIntent = new Intent(this, InboxActivity.class);
    // Tab Content
    inboxSpec.setContent(inboxIntent);

    // Outbox Tab
    TabSpec outboxSpec = tabHost.newTabSpec(OUTBOX_SPEC);
    outboxSpec.setIndicator(OUTBOX_SPEC, getResources().getDrawable(R.drawable.arrow));
    Intent outboxIntent = new Intent(this, ProfileActivity.class);
    outboxSpec.setContent(outboxIntent);

    // Profile Tab
    TabSpec profileSpec = tabHost.newTabSpec(PROFILE_SPEC);
    profileSpec.setIndicator(PROFILE_SPEC, getResources().getDrawable(R.drawable.arrow));
    Intent profileIntent = new Intent(this, LazyAdapter.class);
    profileSpec.setContent(profileIntent);

    // Adding all TabSpec to TabHost
    tabHost.addTab(inboxSpec); // Adding Inbox tab
    tabHost.addTab(outboxSpec); // Adding Outbox tab
    tabHost.addTab(profileSpec); // Adding Profile tab
}
}

CustomizedListView (for output in ListView)

public class CustomizedListView extends Activity {
static final String URL = "https://api.api2cart.com/v1.0/product.list.xml?api_key=6aed775211e8c3d556db063d12125d2d&store_key=ed58a22dfecb405a50ea3ea56979360d&start=0&count=19&params=id,u_model,name,price,images,short_description";
// XML node keys

static final String KEY_SONG = "product"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "u_model";
static final String KEY_ARTIST = "name";
static final String KEY_DURATION = "price";
static final String KEY_THUMB_URL = "http_path";
static final String KEY_SHORT_DESCRIPTION = "short_description";

ListView list;
LazyAdapter adapter;

//@Override
public void onCreate( ) {
    //super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML from URL
    Document doc = parser.getDomElement(xml); // getting DOM element

    NodeList nl = doc.getElementsByTagName(KEY_SONG);
    // looping through all song nodes &lt;song&gt;
    for (int i = 0; i < nl.getLength(); i++) {
        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
        // adding each child node to HashMap key =&gt; value
        map.put(KEY_ID, parser.getValue(e, KEY_ID));
        map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
        map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
        map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
        map.put(KEY_SHORT_DESCRIPTION, parser.getValue(e, KEY_SHORT_DESCRIPTION));
        map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

        // adding HashList to ArrayList
        songsList.add(map);
    }

    list=(ListView)findViewById(R.id.list);

    // Getting adapter by passing xml data ArrayList
    adapter=new LazyAdapter(CustomizedListView.this, songsList);
    list.setAdapter(adapter);

    // Click event for single list row
    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

        }
    });
}
}

LazyAdapter (custom adapter)

public class LazyAdapter extends BaseAdapter {

public Activity activity;
public ArrayList<HashMap<String, String>> data;
public static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyAdapter(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.list_row, null);

    TextView title = (TextView)vi.findViewById(R.id.title); // title
    TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
    TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
    TextView short_description  = (TextView)vi.findViewById(R.id.short_description);
    ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image

    HashMap<String, String> song = new HashMap<String, String>();
    song = data.get(position);

    // Setting all values in listview
    title.setText(song.get(CustomizedListView.KEY_TITLE));
    artist.setText(song.get(CustomizedListView.KEY_ARTIST));
    duration.setText(song.get(CustomizedListView.KEY_DURATION));
    short_description.setText(song.get(CustomizedListView.KEY_SHORT_DESCRIPTION));
    imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
    return vi;
}
}

LogCat:
12-06 19:22:57.576: E/AndroidRuntime(947): FATAL EXCEPTION: main
12-06 19:22:57.576: E/AndroidRuntime(947): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.androidhive/com.example.androidhive.LazyAdapter}: java.lang.InstantiationException: com.example.androidhive.LazyAdapter
12-06 19:22:57.576: E/AndroidRuntime(947): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
12-06 19:22:57.576: E/AndroidRuntime(947): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
12-06 19:22:57.576: E/AndroidRuntime(947): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
12-06 19:22:57.576: E/AndroidRuntime(947): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
12-06 19:22:57.576: E/AndroidRuntime(947): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
12-06 19:22:57.576: E/AndroidRuntime(947): at android.widget.TabHost.setCurrentTab(TabHost.java:323)
12-06 19:22:57.576: E/AndroidRuntime(947): at android.widget.TabHost
$2.onTabSelectionChanged(TabHost.java:129)
12-06 19:22:57.576: E/AndroidRuntime(947): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
12-06 19:22:57.576: E/AndroidRuntime(947): at android.view.View.performClick(View.java:2408)
12-06 19:22:57.576: E/AndroidRuntime(947): at android.view.View$PerformClick.run(View.java:8816)
12-06 19:22:57.576: E/AndroidRuntime(947): at android.os.Handler.handleCallback(Handler.java:587)
12-06 19:22:57.576: E/AndroidRuntime(947): at android.os.Handler.dispatchMessage(Handler.java:92)
12-06 19:22:57.576: E/AndroidRuntime(947): at android.os.Looper.loop(Looper.java:123)
12-06 19:22:57.576: E/AndroidRuntime(947): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-06 19:22:57.576: E/AndroidRuntime(947): at java.lang.reflect.Method.invokeNative(Native Method)
12-06 19:22:57.576: E/AndroidRuntime(947): at java.lang.reflect.Method.invoke(Method.java:521)
12-06 19:22:57.576: E/AndroidRuntime(947): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-06 19:22:57.576: E/AndroidRuntime(947): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-06 19:22:57.576: E/AndroidRuntime(947): at dalvik.system.NativeStart.main(Native Method)
12-06 19:22:57.576: E/AndroidRuntime(947): Caused by: java.lang.InstantiationException: com.example.androidhive.LazyAdapter
12-06 19:22:57.576: E/AndroidRuntime(947): at java.lang.Class.newInstanceImpl(Native Method)
12-06 19:22:57.576: E/AndroidRuntime(947): at java.lang.Class.newInstance(Class.java:1429)
12-06 19:22:57.576: E/AndroidRuntime(947): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
12-06 19:22:57.576: E/AndroidRuntime(947): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
12-06 19:22:57.576: E/AndroidRuntime(947): ... 18 more


Solution

  • You're putting a BaseAdapter subclass on a tab, you should have an Activity instead:

    // Profile Tab
    TabSpec profileSpec = tabHost.newTabSpec(PROFILE_SPEC);
    profileSpec.setIndicator(PROFILE_SPEC, getResources().getDrawable(R.drawable.arrow));
    Intent profileIntent = new Intent(this, **LazyAdapter.class**);   // <--- WRONG
    profileSpec.setContent(profileIntent);
    

    change this to CustomizedListView.class instead of LazyAdapter.class.

    When clicking on the Profile tab, Android expects an Activity subclass to be instantiated, not a BaseAdapter.