Android ArrayAdapter getActivity()返回null

时间:2012-07-27 08:43:34

标签: android android-fragments android-activity android-listfragment

我从片段中获取活动时遇到问题。我在这里阅读了几个主题并且我确信它会起作用,但我仍然在我的片段中的onAttach()方法中从getActivity()方法中获取null。

我的代码:

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) {
    }
}

我打电话。

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

我的片段中的onAttach()方法,如多个线程所述。

我已经尝试了几乎所有的东西,但我不能让它返回Activity。非常感谢您的回答。

日志:

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

我的getView方法

@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;
}

2 个答案:

答案 0 :(得分:3)

onAttach()中,尚未准备好使用该活动..请检查fragment lifecycle ..
您可以将代码移至onActivityCreated()而不是

答案 1 :(得分:0)

我建议以下内容。在扩展Fragment的类中为您的上下文创建一个属性:

private FragmentActivity myContext;

重写以下方法,当片段附加到其上下文时会调用该方法。

@Override
public void onAttach(@NonNull Context context) {
    myContext = (FragmentActivity) context;
    super.onAttach(context);
}