首页 > 解决方案 > 片段中的捆绑为空

问题描述

我正在尝试将数据从 MainActivity 发送到 SunFragment (主要片段):

MainActivity 公共类 MainActivity 扩展 AppCompatActivity { private FusedLocationProviderClient mFusedLocationClient; 私有静态 FragmentManager 片段管理器;

//  protected Location mLastLocation;
private LocationRequest mLocationRequest;
private LocationSettingsRequest mLocationSettingsRequest;
private LocationCallback mLocationCallback;
private Location mLastLocation;

private static final int REQUEST_PERMISSIONS_REQUEST_CODE = 34;
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    fragmentManager = getSupportFragmentManager();//Get Fragment Manager

    //FloatingActionButton fab = findViewById(R.id.fab);
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    Bundle bundle = new Bundle();
    bundle.putDouble("loclat", 25.4358);
    bundle.putDouble("loclang",81.8463);
    Fragment SunFragment = new SunFragment();
    SunFragment.setArguments(bundle);
    fragmentManager.beginTransaction().replace(R.id.view_pager, new SunFragment()).commit();

太阳碎片

public class SunFragment extends Fragment {

    List<SunSession> sunsList;
    Typeface sunfont;
    Double Dlat;
    Double Dlang;

    //to be called by the MainActivity
    public SunFragment() {
        // Required empty public constructor
    }


    private static final String KEY_LOCATION_NAME = "location_name";
    public String TAG ="Sunfragment";
    public String location;//="No location name found";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Retrieve location and camera position from saved instance state.
        if (savedInstanceState != null) {
            location = savedInstanceState.getCharSequence(KEY_LOCATION_NAME).toString();
            System.out.println("OnCreate location  "+location);
           // Dlat = getArguments().getDouble("loclat");
            //Dlang = getArguments().getDouble("loclang");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_sun, container, false);
        onSaveInstanceState(new Bundle());
        if (getArguments() != null){
            Dlat = getArguments().getDouble("loclat");
            Dlang = getArguments().getDouble("loclang");
        }
    Log.e("Lat", Double.toString(Dlat));

SectionPagerAdapter

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    @StringRes
    private static final int[] TAB_TITLES = new int[]{R.string.tab_text_1, R.string.tab_text_2, R.string.tab_text_3};
    private final Context mContext;

    public SectionsPagerAdapter(Context context, FragmentManager fm) {
        super(fm);
        mContext = context;
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        //return PlaceholderFragment.newInstance(position + 1);
        switch (position) {
            case 0:
                return new SunFragment();
            //return PlaceholderFragment.newInstance(pos + 5);
            case 1:
                return PlaceholderFragment.newInstance(1);
            //return SecondFragment.newInstance();
            //return PlaceholderFragment.newInstance(pos + 1);
            default:
                return PlaceholderFragment.newInstance(2);
        }
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return mContext.getResources().getString(TAB_TITLES[position]);
    }

    @Override
    public int getCount() {
        // Show 2 total pages.
        return 3;
    }
}

这是给出错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'double java.lang.Double.doubleValue()' on a null object reference
        at com.example.phocast.ui.main.SunFragment.onCreateView(SunFragment.java:71)

我已经检查了thisthis,这似乎很接近,但无法解决我的问题。我在这里做错了什么?

标签: javaandroidbundle

解决方案


 mainActivity.java
 ============

  SquardFragment   
          squardFragment=SquardFragment.newInstance(matchId2,page,matchStatus);
       FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.replace(R.id.container, squardFragment, mTag);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    if (canAddtoBackStack) transaction.addToBackStack(mTag);
    transaction.commit();


  SquardFragment.java
  =================== 

   public static SquardFragment newInstance(String matchId,
        int page,String matchStatus)    {
    SquardFragment frag = new SquardFragment();
    Bundle argsBundle = new Bundle();
    argsBundle.putString("matchId", matchId);
    argsBundle.putInt("page", page);
    argsBundle.putString("matchStatus", matchStatus);
    frag.setArguments(argsBundle);
    return frag;
  }

  if (getArguments() != null) {
        matchId = getArguments().getString("matchId");
        page = getArguments().getInt("page", 0);
        matchStatus=getArguments().getString("matchStatus");
    }

推荐阅读