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 8292203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:23:29+00:00 2026-06-08T13:23:29+00:00

I have problem with getting the Activity from my Fragment. I have read several

  • 0

I have problem with getting the Activity from my Fragment. I have read several threads here and I am convinced that it shoud work, but I am still getting null from getActivity() method in my onAttach() method in my fragment.

My code:

public static class TabsAdapter extends FragmentPagerAdapter implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
    private final FragmentActivity mContext;
    private final ActionBar mActionBar;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
    private Fragment mFragment;


    static final class TabInfo {
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(Class<?> _class, Bundle _args) {
            clss = _class;
            args = _args;
        }
    }

    public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mActionBar = activity.getSupportActionBar();
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mActionBar.addTab(tab);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mTabs.size();
    }

    @Override
    public Fragment getItem(int position) {
        TabInfo info = mTabs.get(position);
        FragmentTransaction ft=mContext.getSupportFragmentManager().beginTransaction();
        Fragment fragment=Fragment.instantiate(mContext, info.clss.getName(), info.args);
        if(mFragment==null || !mFragment.equals(fragment)){
            mFragment=fragment;
            ft.add(mViewPager.getId(), mFragment);  
            ft.commit();
        }else{
            ft.detach(mFragment);
            mFragment=fragment;
            ft.attach(mFragment);
            ft.commit();
        }           
        return mFragment;
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    }

    @Override
    public void onPageSelected(int position) {
        mActionBar.setSelectedNavigationItem(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        Object tag = tab.getTag();
        for (int i = 0; i < mTabs.size(); i++) {
            if (mTabs.get(i) == tag) {
                mViewPager.setCurrentItem(i);                   
            }
        }


    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {


    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }
}

and I call.

setListAdapter(new CompaniesListAdapter(getActivity(), records));

From my onAttach() method in my Fragment as it is described in several threads.

I have tried almost everything, but I can’t make it return the Activity. Thank you very much in advance for your answers.

log:

07-27 11:30:57.648: E/AndroidRuntime(18378): FATAL EXCEPTION: main
07-27 11:30:57.648: E/AndroidRuntime(18378): java.lang.NullPointerException
07-27 11:30:57.648: E/AndroidRuntime(18378):    at  com.citysmartgo.cheaptaxi.tabs.support.CompaniesListAdapter.getView(CompaniesListAdapter.java:59)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.widget.AbsListView.obtainView(AbsListView.java:2033)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.widget.ListView.makeAndAddView(ListView.java:1772)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.widget.ListView.fillDown(ListView.java:672)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.widget.ListView.fillFromTop(ListView.java:732)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.widget.ListView.layoutChildren(ListView.java:1625)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.widget.AbsListView.onLayout(AbsListView.java:1863)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.view.View.layout(View.java:11278)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.view.View.layout(View.java:11278)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1388)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.view.View.layout(View.java:11278)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.view.View.layout(View.java:11278)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.view.View.layout(View.java:11278)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.view.View.layout(View.java:11278)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.view.ViewGroup.layout(ViewGroup.java:4224)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.os.Looper.loop(Looper.java:137)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at android.app.ActivityThread.main(ActivityThread.java:4424)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at java.lang.reflect.Method.invokeNative(Native Method)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at java.lang.reflect.Method.invoke(Method.java:511)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-27 11:30:57.648: E/AndroidRuntime(18378):    at dalvik.system.NativeStart.main(Native Method)
07-27 11:30:58.874: I/Process(18378): Sending signal. PID: 18378 SIG: 9

my getView method

@Override
public View getView(int position, View convertView, ViewGroup parrent){
    View rowView = convertView;
    TaxiCompanyQuoteView cqView = null;

    if(rowView == null) {
        LayoutInflater inflater = activity.getLayoutInflater();
        rowView = inflater.inflate(R.layout.companies_row_layout, null);

        cqView=new TaxiCompanyQuoteView();
        cqView.company=(TextView) rowView.findViewById(R.id.companies_name);
        cqView.company=(TextView) rowView.findViewById(R.id.companies_phone);
        cqView.company=(TextView) rowView.findViewById(R.id.companies_price_per_unit);
        cqView.company=(TextView) rowView.findViewById(R.id.companies_entering_price);
        cqView.company=(TextView) rowView.findViewById(R.id.companies_waiting_price);

        rowView.setTag(cqView);
    }else{
        cqView = (TaxiCompanyQuoteView) rowView.getTag();
    }

    TaxiCompany currentTaxiCompany = taxiCompanies.get(position);
    cqView.company.setText(currentTaxiCompany.getName());
    cqView.phone.setText(currentTaxiCompany.getNumber());
    cqView.pricePerUnit.setText(currentTaxiCompany.getPricePerUnit());
    cqView.waitingPrice.setText(currentTaxiCompany.getEnteringPrice());
    cqView.enteringPrice.setText(currentTaxiCompany.getWaitingPrice());

    return rowView;
}
  • 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-08T13:23:31+00:00Added an answer on June 8, 2026 at 1:23 pm

    In the onAttach() the activity is not ready to be used yet.. check the fragment lifecycle..
    You can move your code to onActivityCreated() instead

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

Sidebar

Related Questions

I have a problem that I am getting an xml file from raw resource
I have a problem getting boost::multi_index_container work with random-access and with orderd_unique at the
I have this problem getting my newly created php project on Netbeans work on
I'm using jqGrid and I have a problem getting Dynamic Linq to work. I
I have a strange problem getting the color of the point that was touched.
I have a problem with getting my pager-function to work. For some reason it
I have a problem with getting jquery to retrieve results from a WCF service.
I have a problem on going back (clicking the Back button) from sub-activity to
I Have activity that get some data from the internet, and shows it to
I have a problem that I am getting IllegalArgumentException while reading a video file

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.