Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9150055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:33:41+00:00 2026-06-17T11:33:41+00:00

I found a code to implement tab with fragment. I tried to put listview

  • 0

I found a code to implement tab with fragment. I tried to put listview in the fragment, but the application is crashing with the following error. Could you please take a look on the code and guide me. Thank you.

01-18 11:44:49.130: E/AndroidRuntime(1908): FATAL EXCEPTION: main
01-18 11:44:49.130: E/AndroidRuntime(1908): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.abe/com.example.abe.TabsFragmentActivity}: java.lang.NullPointerException
01-18 11:44:49.130: E/AndroidRuntime(1908):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-18 11:44:49.130: E/AndroidRuntime(1908):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-18 11:44:49.130: E/AndroidRuntime(1908):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-18 11:44:49.130: E/AndroidRuntime(1908):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-18 11:44:49.130: E/AndroidRuntime(1908):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 11:44:49.130: E/AndroidRuntime(1908):     at android.os.Looper.loop(Looper.java:137)
01-18 11:44:49.130: E/AndroidRuntime(1908):     at android.app.ActivityThread.main(ActivityThread.java:4745)
01-18 11:44:49.130: E/AndroidRuntime(1908):     at java.lang.reflect.Method.invokeNative(Native Method)
01-18 11:44:49.130: E/AndroidRuntime(1908):     at java.lang.reflect.Method.invoke(Method.java:511)
01-18 11:44:49.130: E/AndroidRuntime(1908):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-18 11:44:49.130: E/AndroidRuntime(1908):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-18 11:44:49.130: E/AndroidRuntime(1908):     at dalvik.system.NativeStart.main(Native Method)
01-18 11:44:49.130: E/AndroidRuntime(1908): Caused by: java.lang.NullPointerException
01-18 11:44:49.130: E/AndroidRuntime(1908):     at com.example.abe.Tab1Fragment.onCreateView(Tab1Fragment.java:44)
01-18 11:44:49.130: E/AndroidRuntime(1908):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
01-18 11:44:49.130: E/AndroidRuntime(1908):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
01-18 11:44:49.130: E/AndroidRuntime(1908):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
01-18 11:44:49.130: E/AndroidRuntime(1908):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
01-18 11:44:49.130: E/AndroidRuntime(1908):     at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1861)
01-18 11:44:49.130: E/AndroidRuntime(1908):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:547)
01-18 11:44:49.130: E/AndroidRuntime(1908):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
01-18 11:44:49.130: E/AndroidRuntime(1908):     at android.app.Activity.performStart(Activity.java:5018)
01-18 11:44:49.130: E/AndroidRuntime(1908):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
01-18 11:44:49.130: E/AndroidRuntime(1908):     ... 11 more

TABFRAGMENTACTIVITY class:

import java.util.HashMap;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabContentFactory;
public class TabsFragmentActivity extends FragmentActivity implements TabHost.OnTabChangeListener {

    private TabHost mTabHost;
    private HashMap<String, TabInfo> mapTabInfo = new HashMap<String, TabsFragmentActivity.TabInfo>();
    private TabInfo mLastTab = null;
    private class TabInfo {
        private String tag;
    private Class<?> clss;
    private Bundle args;
    private Fragment fragment;
    TabInfo(String tag, Class<?> clazz, Bundle args) {
        this.tag = tag;
        this.clss = clazz;
        this.args = args;
    }

    }
    class TabFactory implements TabContentFactory {

        private final Context mContext;
    public TabFactory(Context context) 
    {
        mContext = context;
    }
    public View createTabContent(String tag) 
    {
        View v = new View(mContext);
        v.setMinimumWidth(0);
        v.setMinimumHeight(0);
        return v;
    }

    }
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabs_layout);
        initialiseTabHost(savedInstanceState);
        if (savedInstanceState != null) {
    mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab")); //set the tab as per the saved state
    }
    }
protected void onSaveInstanceState(Bundle outState) {
    outState.putString("tab", mTabHost.getCurrentTabTag()); //save the tab selected
    super.onSaveInstanceState(outState);
}
    private void initialiseTabHost(Bundle args) {
        mTabHost = (TabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup();
    TabInfo tabInfo = null;
    TabsFragmentActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab1").setIndicator("Tab 1"), ( tabInfo = new TabInfo("Tab1", Tab1Fragment.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
    TabsFragmentActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab2").setIndicator("Tab 2"), ( tabInfo = new TabInfo("Tab2", Tab2Fragment.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
    TabsFragmentActivity.addTab(this, this.mTabHost, this.mTabHost.newTabSpec("Tab3").setIndicator("Tab 3"), ( tabInfo = new TabInfo("Tab3", Tab3Fragment.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);
    this.onTabChanged("Tab1");

    mTabHost.setOnTabChangedListener(this);
    }
    private static void addTab(TabsFragmentActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
        tabSpec.setContent(activity.new TabFactory(activity));
    String tag = tabSpec.getTag();
    tabInfo.fragment = activity.getSupportFragmentManager().findFragmentByTag(tag);
    if (tabInfo.fragment != null && !tabInfo.fragment.isDetached()) {
    FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
    ft.detach(tabInfo.fragment);
    ft.commit();
    activity.getSupportFragmentManager().executePendingTransactions();
    }
    tabHost.addTab(tabSpec);
    }
    public void onTabChanged(String tag) {
        TabInfo newTab = this.mapTabInfo.get(tag);
        if (mLastTab != newTab) {
            FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();
    if (mLastTab != null) {
        if (mLastTab.fragment != null) {
            ft.detach(mLastTab.fragment);
        }
    }
    if (newTab != null) {
        if (newTab.fragment == null) {
        newTab.fragment = Fragment.instantiate(this,
            newTab.clss.getName(), newTab.args);
        ft.add(R.id.realtabcontent, newTab.fragment, newTab.tag);
        } else {
        ft.attach(newTab.fragment);
        }
    }
    mLastTab = newTab;
    ft.commit();
    this.getSupportFragmentManager().executePendingTransactions();
        }
}


}

TABFRAGMENT class – this is where i tried to put listview:

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
public class Tab1Fragment extends ListFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        ListView lv = (ListView)getView().findViewById(R.id.listView1);
        String android_array1[] = new String[50];
        for(int i=0;i<50;i++)
        {
            android_array1[i]="ab"+i;
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_1, android_array1);
        lv.setAdapter(adapter);                     
        return (LinearLayout)inflater.inflate(R.layout.tab_frag1_layout, container, false);
    }
}

This is the xml for the tabfragment class:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF0000"
>

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1" >
</ListView>

</LinearLayout>  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:33:42+00:00Added an answer on June 17, 2026 at 11:33 am

    NOTE:

    If you extends ListFragment in Your Tab1Fragment
    then no need to use

    ListView lv = (ListView) root.findViewById(R.id.listView1);

    just Remove it.

    Finally Your onCreateView(..) Method Look Like this:

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
    
            String android_array1[] = new String[50];
            for(int i=0;i<50;i++)
            {
                android_array1[i]="ab"+i;
            }
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_1, android_array1);
    
            setListAdapter(adapter);        
    
            return super.onCreateView(inflater, container, savedInstanceState);
        } 
    

    For More information Refer this Link

    http://wptrafficanalyzer.in/blog/creating-navigation-tabs-using-tabhost-and-fragments-in-android/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently I've found my self to implement code following a pattern like: public class
I tried to implement the code found in this link location find The truth
I am trying to implement openssl code I found into my project but I
I'm trying to implement a batch delete. I found this code on the internet:
I found the following code sniplet here: NSURL *url = [NSURL URLWithString:@itms-apps://itunes.com/apps/???]; [[UIApplication sharedApplication]
Im trying to implement some code i found on a website which duplicates a
I found a code snippet which I want to implement. Now is the problem
I'm trying to implement code for nested commenting which I found at http://www.jongales.com/blog/2009/01/27/php-class-for-threaded-comments/ .
I've found some code that solves a sudoku puzzle, but I only understand part
I've been attempting to implement the QuickActions UI Pattern in my application. I've found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.