问题描述
大家好我开发一个应用程序,以解释我创建简单的插画我的处境。当我点击另一个活动里面的按钮,我想将数据发送到片段。我用它捆绑给我NullPointerException异常。我试图用方法的另一个活动再次给了我的错误中。如何从活动传递数据提前到碎片的感谢。
Hello Everyone I'm developing an app,in order to explain my situation I've created simple illustrator. When i click the button inside another activity i want to send data to fragment. I use bundle its gives me nullpointerexception. I tried to use method inside another activity it gives me error again. How can i pass data from Activity to Fragment thanks in advance.
下面我codeS。
EDIT1:我想我解释错了。瞧,我已经上传的照片。当我点击白色按钮anotheractivity正在打开。当我打字里面anotheractivity一些文字,然后点击里面的anotheractivity按钮,然后我想送DATAS片段类。我希望我能解释的。谢谢你。
Edit1 : I think i explained wrong. Look at photo that i've uploaded. When i click the white button anotheractivity is opening. When i typing some text inside anotheractivity and click the button inside anotheractivity, then i want to send datas Fragment class. I hope i was able to explained. Thanks.
MainActivity
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,AnotherActivity.class);
startActivity(i);
}
});
}
}
AnotherActivity
public class AnotherActivity extends Activity {
EditText et;
Button btn;
public String ets;
FragmentClass myFragment = new FragmentClass();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.anotheractivity);
btn = (Button) findViewById(R.id.button);
et= (EditText) findViewById(R.id.editText);
ets = et.getText().toString();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myFragment.Test(ets);
}
});
}
}
片段
public class FragmentClass extends Fragment {
TextView tv;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment,container,false);
tv = (TextView)v.findViewById(R.id.textView);
return v;
}
public void Test(String name){
Toast.makeText(getActivity(),name,Toast.LENGTH_SHORT).show();
}
}
推荐答案
这是活动和片段之间沟通的最佳实践
public class TestFragment extends Fragment {
private static final String ATAG = "atag";
public static TestFragment getFragment(int value){
TestFragment fragment = new TestFragment();
Bundle bundle = new Bundle();
bundle.putInt(ATAG, 100);
fragment.setArguments(bundle);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_fragment, container,false);
Bundle b = getArguments();
if(b!= null)
YOUR DATA = b.getInt(ATAG);
return view;
}
}
和活动:
public class MainActivity extends Activity{
private static final String FOO = "foofragment";
TestFragment newFragment;
Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState == null){
newFragment = TestFragment.getFragment(100);
}else {
newFragment = (TestFragment) getFragmentManager().findFragmentByTag(FOO);
}
mButton.setOnClickListener(new OnClickListener() {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.container, newFragment, FOO);
transaction.commit();
}
}
}
这篇关于活动和片段之间的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!