【问题标题】:Android send info from Activity to its fragmentAndroid 将信息从 Activity 发送到其 Fragment
【发布时间】:2017-05-10 09:30:08
【问题描述】:

我有一个活动,它正在从其他活动中获取一些字符串:

public class ShowPoints extends AppCompatActivity {

    public String points;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_points);

        Intent i = getIntent();
        points = i.getStringExtra("geoPoints");
    }
}

这行得通。

从这个活动ShowPoints,我想将接收到的字符串points发送到其名为PointsOnMap的片段

为此,我创建了片段 PointsOnMap 并这样做了:

public class PointsOnMap extends Fragment implements OnMapReadyCallback {
      ......
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        act = new ShowPoints();
        String points = act.points;
        mView = inflater.inflate(R.layout.activity_points_on_map, container, false);
        return mView;
    }

但片段PointsOnMapString points 从未填充来自活动ShowPoints 的字符串。

我想知道我做错了什么。我在这里阅读了其他一些帖子,似乎我的做法是正确的。

----编辑----

我对代码所做的最后一次编辑似乎不起作用,我很确定问题出在我传递参数的部分,因为在没有传递参数的情况下,一切正常,并且显示了初始映射。

所以到目前为止,包含该类的活动是这样的:

public class ShowPoints extends AppCompatActivity {

    public String points;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_points);

        Intent i = getIntent();
        points = i.getStringExtra("geoPoints");

        PointsOnMap pointsOnMapFragment = new PointsOnMap();
        Bundle bundle = new Bundle();
        bundle.putString("geoPoints",points);
        pointsOnMapFragment.setArguments(bundle);

        Log.d("REACHED HERE","---------");
    }
}

我正在检索传递的字符串的片段部分是这样的:

public class PointsOnMap extends Fragment implements OnMapReadyCallback {
      ......
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        Bundle args = getArguments();
        geopoints = args.getString("geoPoints");
        mView = inflater.inflate(R.layout.activity_points_on_map, container, false);

        return mView;
    }

由于某种原因它仍然无法正常工作......回溯表明null object reference在线片段上的geopoints = args.getString("geoPoints");

【问题讨论】:

  • 因为它显示的是点变量的垃圾值
  • 您无法实例化您的活动。而不是在为片段进行交易时使用setArguments(bundle)
  • 嘿 AlphaQ,我知道它可能是重复的,但正如我在下面评论的那样,我已经尝试了其他一些方法,比如在那篇文章中的 Bundle,但它没有用....无论如何,我会给它再试一次
  • 请发布最新代码

标签: android android-fragments android-activity


【解决方案1】:

当您从 ShowPoints 活动中替换/添加 PointsOnMap 片段时:

PointsOnMap pointsOnMapFragment = new PointsOnMap();
    Bundle bundle = new Bundle();
    bundle.putString("geoPoints",geoPoints);
    pointsOnMapFragment.setArguments(bundle);

并在片段使用时获取 geoPoint:

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String geoPoint = getArguments().getString("geoPoints");
}

为了在活动之间传递数据,我们使用意图对象,为了从活动到片段或片段到片段传递数据,我们使用片段的 setArgument(Bundle bundle) 方法来携带数据。

【讨论】:

    【解决方案2】:

    如果你想在片段创建时传递信息,你需要调用 setArgument,这里是一个例子 https://stackoverflow.com/a/17436739/6726261

    如果您需要在创建片段后传递数据,您可以在片段中定义回调函数以应用 Android doc 有一个很好的例子 https://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

    【讨论】:

      【解决方案3】:

      我之前也遇到过同样的问题,在同一个线程中传递意图数据时您可能会遇到很多问题,所以我建议将您的数据存储到SharedPrefrence,然后使用Intent 传递它,例如:

      public String points;
      Intent i = getIntent();
      SharedPreferences settings = getSharedPreferences("pref", MODE_PRIVATE);
      SharedPreferences.Editor editor = settings.edit();
      editor.putString("geoPoints", "geoPoints");
      editor.commit();`
      points = i.getStringExtra(getSharedPreferences("pref",MODE_PRIVATE).getString("geoPoints",""));
      

      【讨论】:

        【解决方案4】:

        分片发送数据,可以选择两种方式

        方法一 在 Fragment 中使用 Bundle 和 setArguments:

        Bundle bundle = new Bundle();
        bundle.putString("data", "Val");
        // set Arguments
        FragmentClass frag = new FragmentClass();
        frag.setArguments(bundle);
        

        在 Fragment onCreatView() 中调用 getArgument 就像在 Activity 中的 getIntent 一样

        Bundle bundle = getArguments();
        

        方法二 在构造函数中发送数据

        Bundle bundle = new Bundle();
        bundle.putString("data", "Val");
        // set Arguments
        user new FragmentClass().newInstance(bundle);
        

        在片段类中:

        public static FragmentClass newInstance(Bundle bundle) {
            FragmentClass frag = new FragmentClass();
            frag.setArguments(bundle);
        
            return frag;
        }
        
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            Bundle bundle = getArguments();    
            return inflater.inflate(R.layout.fragment, container, false);
        }
        

        【讨论】:

        • 是的,我在另一篇文章中也读到了这个 Bundle 方法,但由于某种原因它不起作用......也许我做错了什么......我会再试一次,谢谢。跨度>
        【解决方案5】:

        将数据从Activity传递到Fragment

        ShowPoints

        Intent i = getIntent();  // receive from others activity
        points = i.getStringExtra("geoPoints");
        
        Bundle bundle = new Bundle(); // ready to pass to fragment 
        bundle.putString("points", points);
        // set Fragmentclass Arguments
        PointsOnMap fragobj = new PointsOnMap();
        fragobj.setArguments(bundle);
        

        PointsOnMap片段中

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            String strtext = getArguments().getString("points"); // receive from Activity  ShowPoints
            return inflater.inflate(R.layout.fragment, container, false);
        }
        

        编辑(PointOnMaps)

          public class PointsOnMap extends Fragment implements OnMapReadyCallback {
              ......
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        
                View mView = inflater.inflate(R.layout.activity_points_on_map, container, false);
        
                Bundle bundle=this.getArguments();
                if(getArguments()!=null)
                {
                   geopoints = bundle.getString("geoPoints");
                }
        
                return mView;
            } 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-02-07
          • 1970-01-01
          • 2013-09-27
          • 2018-04-14
          相关资源
          最近更新 更多