片段与其他活动之间的交流 [英] communication between fragment and other activity

查看:40
本文介绍了片段与其他活动之间的交流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 公共类FragActivity1扩展Fragment实现View.OnClickListener {视图视图;ImageButton name_change__btn,status_change_btn,profile_change_btn;ImageView person_dp;TextView status_field,name_field;....一些代码在这里 

我有一个包含changeName方法的类,该方法正在更改textview的文本.name_field的写法是:

  name_field =(TextView)view.findViewById(R.id.name_field); 

现在,我从一个弹出按钮调用此方法,在此我在运行时传递一些字符串值.

我正在得到nullpointer异常.

  public void changeName(String s){Log.d(更改名称条目",更改名称网");System.out.println(s);System.out.println(name_field.getText().toString());name_field.setText(s);System.out.println(name_field.getText());} 

这是弹出窗口类的代码:

 公共类Name_Status扩展了AppCompatActivity实现的View.OnClickListener {EditText name_change;RelativeLayout name_status_edit_field;字符串名称;按钮cancel_name_change,ok_name_change;@Override受保护的void onCreate(@Nullable Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.name_status);DisplayMetrics dm = new DisplayMetrics();getWindowManager().getDefaultDisplay().getMetrics(dm);int width = dm.widthPixels;int height = dm.heightPixels;getWindow().setLayout((int)(width * .8),(int)(height * .3));name_change =(EditText)findViewById(R.id.change_name);name_status_edit_field =(RelativeLayout)findViewById(R.id.name_status_edit_field);cancel_name_change =(按钮)findViewById(R.id.cancel_change_name);ok_name_change =(按钮)findViewById(R.id.ok_change_name);ok_name_change.setOnClickListener(this);name_change.setOnClickListener(this);name_status_edit_field.clearFocus();name_change.setFocusable(true);//name_change.update();}@Overridepublic void onClick(View v){if(v.getId()== R.id.ok_change_name){name = name_change.getText().toString();System.out.println(name);Log.d("entered","entred");FragActivity1 obj = new FragActivity1();Log.d("obj created","obj created");obj.changeName(name);Log.d("obj.changename","obj.changename");Intent intent = new Intent();Log.d("intenthsjjsjs","intent hxjjx");intent.setClass(Name_Status.this,MainActivity.class);startActivity(intent);}}} 

我什至进行搜索,发现片段无法与其他片段或活动直接通信.但是听不懂.除包含我的片段的活动之外,此弹出窗口是一项新活动.那么如何在片段和其他活动之间进行交流呢?

希望我能很好地解释我的问题.请尝试使用外行语言进行解释.

解决方案

此处.基本上,您将数据放入 Bundle 中,然后使用 putExtras()将其插入 ntent 中.

在您的情况下,您应该首先使该片段与 its 活动进行通信,然后从此处从 Bundle 开始新的活动.

public class FragActivity1 extends Fragment implements View.OnClickListener {

    View view;
    ImageButton name_change__btn, status_change_btn, profile_change_btn;
    ImageView person_dp;
    TextView status_field , name_field;
    .
    .
    . 
    .some code here

I've this class containing changeName method which is changing text of a textview . name_field is written as:

name_field=(TextView)view.findViewById(R.id.name_field);

Now I'm calling this method from a popup button where i'm passing some string value at runtime.

I'm getting nullpointer exception.

 public void changeName(String s)
{

    Log.d("changename entry" ,"changename netry");
    System.out.println(s);
    System.out.println(name_field.getText().toString());
       name_field.setText(s);
    System.out.println(name_field.getText());
}

Here is the code of popup class:

public class Name_Status extends AppCompatActivity implements View.OnClickListener {

    EditText name_change;
    RelativeLayout name_status_edit_field;
    String name;
    Button cancel_name_change , ok_name_change;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.name_status);
         DisplayMetrics dm=new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        int width=dm.widthPixels;
        int height=dm.heightPixels;

        getWindow().setLayout((int)(width*.8),(int)(height*.3));
        name_change=(EditText)findViewById(R.id.change_name);
        name_status_edit_field=    (RelativeLayout)findViewById(R.id.name_status_edit_field);

        cancel_name_change=(Button)findViewById(R.id.cancel_change_name);
        ok_name_change=(Button)findViewById(R.id.ok_change_name);
        ok_name_change.setOnClickListener(this);
        name_change.setOnClickListener(this);
       name_status_edit_field.clearFocus();
        name_change.setFocusable(true);
        //name_change.update();
    }

    @Override
    public void onClick(View v) {

               if(v.getId()==R.id.ok_change_name)

               {
                   name=name_change.getText().toString();
                   System.out.println(name);
                   Log.d("entered","entred");
                   FragActivity1 obj=new FragActivity1();
                   Log.d("obj created", "obj created");
                   obj.changeName(name);
                   Log.d("obj.changename","obj.changename");
                   Intent intent=new Intent();
                   Log.d("intenthsjjsjs","intent hxjjx");
                   intent.setClass(Name_Status.this , MainActivity.class);
                   startActivity(intent);
               }
    }
}

I even searched and found that fragments can't communicate directly with other fragment or activity. But couldn't understand well. And this popup is a new activity other than the one containing my fragment. So how to communicate between fragment and other activity?

Hope I could explain my problem well. Please try to explain in layman language.

解决方案

Communication between fragments and the activity they are attached to is explained here. From the fragment to the activity you simply use getActivity for a reference to the activity. From the activity to the fragment you can use the findFragmentById(R.id.fragment_id) method of the FragmentManager for a reference to the fragment you want to communicate with.

Communication between activities is realized through Intent s and their Extras as described here. You basically put your Data in a Bundle and insert it in the Ìntent with putExtras().

In your case you should first have the fragment communicate with its activity and then start a new activity with a Bundle from here.

这篇关于片段与其他活动之间的交流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆