切换到FragmentTabHost时出现NullPointerException

时间:2014-06-14 15:41:23

标签: android nullpointerexception android-fragmentactivity fragment-tab-host

我正在编写一个应用程序,通过bluetooth连接到微控制器。连接一切都很好。

我的问题是处理页面。

在我的MainActivity中,这是一个FragmentActivity我用FragmentTabHost实现了4个Tabs:

    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this , getSupportFragmentManager() , R.id.realtabcontent);

    tabIndicator1 = LayoutInflater.from(this).inflate(R.layout.apptheme_tab_indicator_holo , mTabHost.getTabWidget() , false);
    title = (TextView) tabIndicator1.findViewById(android.R.id.title);
    title.setText("Home");

    tabIndicator2 = LayoutInflater.from(this).inflate(R.layout.apptheme_tab_indicator_holo , mTabHost.getTabWidget() , false);
    title = (TextView) tabIndicator2.findViewById(android.R.id.title);
    title.setText("Infos");

    tabIndicator3 = LayoutInflater.from(this).inflate(R.layout.apptheme_tab_indicator_holo , mTabHost.getTabWidget() , false);
    title = (TextView) tabIndicator3.findViewById(android.R.id.title);
    title.setText("Development");

    tabIndicator4 = LayoutInflater.from(this).inflate(R.layout.apptheme_tab_indicator_holo , mTabHost.getTabWidget() , false);
    title = (TextView) tabIndicator4.findViewById(android.R.id.title);
    title.setText("Charging");

    mTabHost.addTab(mTabHost.newTabSpec("home").setIndicator(tabIndicator1) , HomeFragment.class , null);
    mTabHost.addTab(mTabHost.newTabSpec("infos").setIndicator(tabIndicator2) , InformationTabs.class , null);
    mTabHost.addTab(mTabHost.newTabSpec("development").setIndicator(tabIndicator3) , DevelopmentTab.class , null);
    mTabHost.addTab(mTabHost.newTabSpec("charging").setIndicator(tabIndicator4) , ChargingTab.class , null);

其中一个标签是InformationTabs。这是一个Fragment本身并实现了一个ViewPager,通过FragmentPagerAdapter滑过3个页面。

@ Override
public View onCreateView(LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState)
{
    infoTabsView = inflater.inflate(R.layout.layout_informations_tab , container , false);

    pageAdapter = new PageAdapter(getChildFragmentManager());

    viewPager = (ViewPager) infoTabsView.findViewById(R.id.viewpager);
    viewPager.setAdapter(pageAdapter);

    viewPager.setPageTransformer(true , new CardTransformer(0.7f));


    // Attach the page change listener inside the activity
    viewPager.setOnPageChangeListener(new OnPageChangeListener()
    {

        // This method will be invoked when a new page becomes selected.
        @ Override
        public void onPageSelected(int position)
        {
            position = position + 1;
            Toast.makeText(getActivity() , "Page: " + position , Toast.LENGTH_SHORT).show();
        }

        // This method will be invoked when the current page is scrolled
        @ Override
        public void onPageScrolled(int position , float positionOffset , int positionOffsetPixels)
        {
            // Code goes here
        }

        @ Override
        public void onPageScrollStateChanged(int state)
        {
            // Code goes here
        }
    });

    return infoTabsView;
}

我现在的问题是,当我启动应用程序时,当我在InfromationsTabs时,一切正常,但是一旦我跳转到其他FragmentTabHost,例如Tab HomeFragment然后回到{ {1}}我得到InformationsTab

logcat的:

NullPointerException

InfosStatus片段

06-14 17:30:35.654: W/dalvikvm(11375): threadid=1: thread exiting with uncaught exception (group=0x2b542210)
06-14 17:30:35.654: E/AndroidRuntime(11375): FATAL EXCEPTION: main
06-14 17:30:35.654: E/AndroidRuntime(11375): java.lang.NullPointerException
06-14 17:30:35.654: E/AndroidRuntime(11375):    at de.teamgrimm.android.crohmapp.fragmente.InfosStatus.onCreateView(InfosStatus.java:61)
06-14 17:30:35.654: E/AndroidRuntime(11375):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
06-14 17:30:35.654: E/AndroidRuntime(11375):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
06-14 17:30:35.654: E/AndroidRuntime(11375):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
06-14 17:30:35.654: E/AndroidRuntime(11375):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
06-14 17:30:35.654: E/AndroidRuntime(11375):    at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1861)
06-14 17:30:35.654: E/AndroidRuntime(11375):    at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1474)
06-14 17:30:35.654: E/AndroidRuntime(11375):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
06-14 17:30:35.654: E/AndroidRuntime(11375):    at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1264)
06-14 17:30:35.654: E/AndroidRuntime(11375):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:672)
06-14 17:30:35.654: E/AndroidRuntime(11375):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
06-14 17:30:35.654: E/AndroidRuntime(11375):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
06-14 17:30:35.654: E/AndroidRuntime(11375):    at android.os.Handler.handleCallback(Handler.java:605)
06-14 17:30:35.654: E/AndroidRuntime(11375):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-14 17:30:35.654: E/AndroidRuntime(11375):    at android.os.Looper.loop(Looper.java:137)
06-14 17:30:35.654: E/AndroidRuntime(11375):    at android.app.ActivityThread.main(ActivityThread.java:4441)
06-14 17:30:35.654: E/AndroidRuntime(11375):    at java.lang.reflect.Method.invokeNative(Native Method)
06-14 17:30:35.654: E/AndroidRuntime(11375):    at java.lang.reflect.Method.invoke(Method.java:511)
06-14 17:30:35.654: E/AndroidRuntime(11375):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-14 17:30:35.654: E/AndroidRuntime(11375):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-14 17:30:35.654: E/AndroidRuntime(11375):    at dalvik.system.NativeStart.main(Native Method)
06-14 17:30:35.764: D/dalvikvm(11375): GC_CONCURRENT freed 660K, 7% free 10790K/11527K, paused 2ms+8ms
  

这是它崩溃的地方##

@ Override
public View onCreateView(LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState)
{

    info2View = inflater.inflate(R.layout.fragment_status , container , false);
    if(info2View != null)
    {
        if(savedInstanceState != null)
        {
            Object[] fields = Define_Hash.ValueViewInfo.values().toArray();
            for(int i = 0; i < fields.length; i ++ )
            {

                TextView d = (TextView) info2View.findViewById((Integer) fields[i]);
                if(d != null)
                    d.setText(savedInstanceState.getString(String.valueOf(fields[i])));

            }
        }

        if(Values != null && Values.size() > 0)
        {
            // read from saved values
            Object[] keys = Values.keySet().toArray();

            for(int i = 0; i < keys.length; i ++ )
            {

0 个答案:

没有答案