简体   繁体   English

从3rdfragment选项卡过渡到2nd / 1stfragment选项卡,中断了应用程序

[英]Transition from 3rd fragment tab to 2nd/1st fragment tab , breaks the app

I am able to do transition between 3 tabs where each class is extending Fragment. 我可以在3个选项卡之间进行过渡,每个类都在其中扩展Fragment。

My fragment A holds the map , fragment B and C are blank with some default background. 我的片段A保存了地图,片段B和C为空白,并带有一些默认背景。 When I run my application , Fragment A loads with the map and upon transition to fragment b and c background color changes indicating a success . 当我运行我的应用程序时,片段A会加载地图,并在过渡到片段b和c时,背景颜色会更改,表明成功。 But on transition back to fragment a crashes the app . 但是在过渡回碎片时,应用程序崩溃了。

Fragment A: 片段A:

package com.example.hspot2;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class FragmentA extends Fragment{

    static final LatLng HAMBURG = new LatLng(53.558, 9.927);
    GoogleMap map;
    MapView mapView;
    SupportMapFragment fragment;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {     
        View v =  inflater.inflate(R.layout.fragment_a, container, false);
        map = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        return v;
    }
}

fragment B : 片段B:

package com.example.hspot2;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentB extends Fragment {

        @Override

        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_b, container, false);

    }
}

fragment c: 片段c:

package com.example.hspot2;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class FragmentC extends Fragment {
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_c, container, false);
        }


}

mainactivity.java : mainactivity.java:

package com.example.hspot2;

import java.util.Locale;

import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;
    ActionBar actionBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mViewPager = (ViewPager) findViewById(R.id.pager);

        actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);


        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mSectionsPagerAdapter);

        for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
            actionBar.addTab(actionBar.newTab()
                    .setText(mSectionsPagerAdapter.getPageTitle(i))
                    .setTabListener(this));
        }
        // When swiping between different sections, select the corresponding
        // tab. We can also use ActionBar.Tab#select() to do this if we have
        // a reference to the Tab.
        mViewPager
                .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        actionBar.setSelectedNavigationItem(position);
                    }
                });


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onTabSelected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in
        // the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab,
            FragmentTransaction fragmentTransaction) {
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            Fragment fragment = null;
            if(i==0){
                   fragment = new FragmentA(); 

            }
            if(i==1){
                  fragment = new FragmentB();
            }
            if(i==2){
                   fragment = new FragmentC();
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            Locale l = Locale.getDefault();
            switch (position) {
            case 0:
                return getString(R.string.title_section1).toUpperCase(l);
            case 1:
                return getString(R.string.title_section2).toUpperCase(l);
            case 2:
                return getString(R.string.title_section3).toUpperCase(l);
            }
            return null;
        }
    }
}

When a Tab is selected ViewPager load the associated Fragment and the Fragments close to it (tab-1 and tab+1) for caching purpose. 选择选项卡后,ViewPager会加载关联的片段及其附近的片段(选项卡1和选项卡1)以进行缓存。 So passing from third one to second/one tab means ViewPAger load always both fragments (1 and 2) . 因此,从第三个选项卡传递到第二个/一个选项卡意味着ViewPAger始终加载两个片段(1和2)。 Because 2 is a void Fragment I suggest to concentrate on first Fragment and I suppose your findFragmentById(R.id.map)) return null and so getMap throw exception. 因为2是一个无效的Fragment,所以我建议集中精力在第一个Fragment上,我想你的findFragmentById(R.id.map))返回null,因此getMap抛出异常。 You have to identify differently your Fragment 您必须以其他方式识别您的片段

try : mViewPager.setOffScreenPageLimit(4); 试试: mViewPager.setOffScreenPageLimit(4); // since 3 is the toal no. //因为3是总次数。 of swipe tabs above 上方的滑动标签

Don't know if this is really efficient way to solve this problem. 不知道这是否是解决此问题的真正有效方法。 Any comment and discussion ? 有什么意见和讨论吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从后台堆栈中的第 3 个片段到第 1 个片段:Android - Going from 3rd to 1st fragment in the backstack: Android 将用户单击/滑动的项目从第一个片段的一个列表视图转移到第二个片段的另一个列表视图 - Transferring user clicked/swiped item from one listview in 1st fragment to another listview in 2nd fragment 从适配器单击第一个选项卡recyclerview项时,如何为第二个选项卡设置动画? - how to animate to 2nd tab when 1st tab recyclerview item is clicked from adapter? 在 android 中将数据从第一个选项卡传递到第三个选项卡活动时获取 null object 引用 - Getting null object reference when passing data from 1st tab to 3rd tab activity in android 如何填充基于第一旋转器的第二旋转器和基于第二旋转器的第三旋转器? - How to populate 2nd spinner based on 1st spinner and 3rd spinner based on 2nd spinner? 如何从活动第3过渡到第1? - How to transition from activity 3rd to 1st? 在ViewPager中,在滑动到第3个选项卡时,tab1中的子片段消失 - In ViewPager, on sliding to 3rd tab, child fragment in tab1 disappears 获取空对象引用如果我在第三项活动之前开始第二项活动(如果我直接从第一项移到第三项,则没有错误) - Getting null object reference If i start 2nd activity before 3rd activity (No error if I move from 1st to 3rd directly) 如何解决获取#2nd记录而不是#1st记录的光标(#3rd而不是#2nd等)? - How to fix cursor that is taking #2nd record instead of #1st (#3rd instead of #2nd etc.)? 在View分页器片段的第一个选项卡中调用的第三个选项卡中的asyncTask - asyncTask from 3rd tab called in first tab in View pager fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM