在片段和活动之间传递数据获得零点异常

时间:2014-07-09 01:07:28

标签: android android-fragments android-fragmentactivity

你好Frends我正在开启我的应用程序的向导,在下一页的那个按钮上应该启用当name有一个单词且mob数为legth为10.然后在Fragment Activty中的那个按钮应该启用这里查看我的代码:

片段活动

public class Intro_Main extends FragmentActivity implements ButtonenableListner{
DataModel data;
Button next;
Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.l_intro_main);
    context=this;
    data=new DataModel();
    next=(Button)findViewById(R.id.button);
    if(findViewById(R.id.fm)!=null)
    {
    Step1 s1=new Step1();
        s1.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction().add(R.id.fm,s1).commit();
    }



}
public void setname(String s){
    data.Name=s;
}
public void setnumber(String s){
    data.Number=s;
}
public void goto2(View v)
{

    Step2 s2 =new Step2();
    s2.setArguments(getIntent().getExtras());
    FragmentManager fragmentManager;
    fragmentManager = getSupportFragmentManager();

    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit);


    transaction.replace(R.id.fm, s2 );
    transaction.addToBackStack(null);
    transaction.commit();
}




@Override
public void enableButton(Boolean b) {
    if (b)
        next.setEnabled(true);
    else
           next.setEnabled(false);
}

}

片段

public class Step1 extends android.support.v4.app.Fragment{
EditText name,mob;
public String s;
public ButtonenableListner listner;
Boolean s1,s2=false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.f_step_1, container, false);
    name=(EditText)view.findViewById(R.id.editText);
    mob=(EditText)view.findViewById(R.id.editText2);

    name.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

            if (i3 != 0)
            {
                s2=true;
            }
            else
                s2=false;
            checkbothfield();
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });
    mob.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {

            if (i3==10)
            {
                s2=true;
            }
            else
                s2=false;
            checkbothfield();
        }

        @Override
        public void afterTextChanged(Editable editable) {


        }
    });

    return view;
}

private void checkbothfield() {
    if (s1==true && s2==true)
    listner.enableButton(true);
    else
        listner.enableButton(false);
}

@Override
public void onStop() {
    DataModel.Name=name.getEditableText().toString();
    DataModel.Number=mob.getEditableText().toString();

    super.onStop();
}


@Override
public void onPause() {
    DataModel.Name=name.getEditableText().toString();
    DataModel.Number=mob.getEditableText().toString();
    super.onPause();
}

}

请帮助我被困在这里谢谢你..

1 个答案:

答案 0 :(得分:0)

我没有正确理解你的问题,但我认为你需要将数据发送回Fragment活动。我这样做的方法是在fragmentActivity中创建一个公共布尔值,并为它们创建getter和setter函数。

public boolean dataFilled;

public boolean isDataFilled() {
    return dataFilled;
}

public void setDataFilled(boolean dataFilled) {
    this.dataFilled = dataFilled;
}

最初使用set this为false。

然后在片段中使用getActivity()获取活动并将其转换为FragmentActivity并调用setter函数:

((Intro_main)getActivity).setDataFilled(true);

返回时,只需检查布尔值。

** 如果您需要将数据从fragmentActivity发送到片段:

使用:

调用新的Step1和参数时
Bundle bd = new Bundle;
bd.addString(key,value);

在片段OnCreate中使用getArguments()获取包并提取数据。