其他方法

时间:2016-06-06 17:41:27

标签: java android android-fragments android-edittext object-reference

myfragment class 中扩展片段我在 onCreateView 方法

中找到对象引用
private static final String TAG = "CloadLab";


private Cloud dCloud;
private EditText dNameText;
public String dNameHolder = "";




@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);

    UUID dreamId = (UUID)getArguments().getSerializable(EXTRA_DREAM_ID);




}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    getActivity().setTitle("Add new");

}

@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup parent, Bundle savedInstanceState){

    View v = inflater.inflate(R.layout.fragment_add,parent,false);

    dNameText = (EditText) v.findViewById(R.id.dream_name);


    return v;
}

public void SaveDream (){


    dNameHolder = dNameText.getText().toString();

    if (dNameText != null) {
        Log.e(TAG,"not null");
    }else if (dNameText == null){
        Log.e(TAG,"null");
    }


}

}

如果我在 onCreateView 方法中使用它可以正常工作,但是例如我想在此类方法中使用 dNameText

public void SaveDream (){
   dNameHolder = dNameText.getText().toString();
}

现在它有null对象引用我该如何解决这个问题?如何通过整个类设置引用?

我在Activity中调用此 SaveDream()方法来保存此片段

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

        case R.id.action_favorite:

            AddFragment mActivity= new AddFragment();
            mActivity.SaveDream();
            finish();
            return true;
    }

    return super.onOptionsItemSelected(item);


}

每个活动扩展 SingleFragmentActivity 并在其中包含这些方法

        private AddFragment mFragment;

@Override
    protected Fragment createFragment() {

    UUID dreamId = (UUID)getIntent()
            .getSerializableExtra(AddFragment.EXTRA_DREAM_ID);
    return AddFragment.newInstance(dreamId);
}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        //setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.action_bar_menu,menu);

        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

            case R.id.action_favorite:

               // AddFragment mActivity= new AddFragment();
               // mActivity.SaveDream();
                //finish();
                mFragment = new AddFragment();
                FragmentManager fragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(android.R.id.content, mFragment);
                fragmentTransaction.commit();
                mFragment.SaveDream();
                finish();
                return true;
        }

        return super.onOptionsItemSelected(item);


    }

SingleFragmentActivity

    protected abstract Fragment createFragment();

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fragment);
    FragmentManager fm = getSupportFragmentManager();
    Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);

   // getSupportActionBar().setDisplayHomeAsUpEnabled(false);


    if (fragment == null){
        fragment = createFragment();
        fm.beginTransaction()
                .add(R.id.fragmentContainer, fragment)
                .commit();
    }

}

1 个答案:

答案 0 :(得分:1)

您正在片段的新实例上调用SaveDream方法,而不是现在在

加载的方法
AddFragment mActivity= new AddFragment();
mActivity.SaveDream();

因为视图未在新实例上创建(视图在加载时创建),并且您为EditText获取空值。您应该全局保存已加载的实例,并在该实例上调用该方法。

请按照以下步骤操作。

第1步:创建类级变量

private AddFragment mFragment;

第2步:像这样加载片段

mFragment = new AddFragment();

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(android.R.id.content, mFragment);
fragmentTransaction.commit();

第3步:像这样调用方法

mFragment.SaveDream();

<强>更新

像这样更新方法

@Override
protected Fragment createFragment() {
    UUID dreamId = (UUID)getIntent()
        .getSerializableExtra(AddFragment.EXTRA_DREAM_ID);
    mFragment = AddFragment.newInstance(dreamId);
    return mFragment;
}

case R.id.action_favorite:
    mFragment.SaveDream();
    return true;