Android将Activity从Activity发送到其片段

时间:2017-05-10 09:30:08

标签: android android-fragments android-activity

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

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

但片段String points的{​​{1}}从未填充来自活动PointsOnMap的字符串。

我想知道我做错了什么。我在这里阅读了其他一些帖子,看起来我正在以正确的方式进行。

---- ---- EDIT

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

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

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

5 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

之前我遇到过同样的问题,在同一个帖子中传递意图数据时可能会遇到很多问题,所以我建议将数据存储到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",""));

答案 2 :(得分: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");
}

为了将数据从活动传递到活动,我们使用intent对象,并且为了将数据从活动传递到片段OR片段到片段,我们使用Fragment的setArgument(Bundle bundle)方法来传输数据。

答案 3 :(得分:1)

要以片段形式发送数据,您可以选择两种方法

方法1 在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();

方法2 在构造函数

中发送数据
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);
}

答案 4 :(得分:0)

将数据从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;
    }