我有两个片段(MainFragment和AnotherFragment)和MainActivity,如下所述。
在" onCreate" MainActivity,我正在添加MainFragment。单击后,我将添加AnotherFragment,它将数据存储在两个editText字段中,并将它们传递给mainfragment,它显示在textView字段中。
MainFragment.java
Button a;
TextView t1,t2;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
a = (Button) getActivity().findViewById(R.id.button);
t1 = (TextView) getActivity().findViewById(R.id.textView);
t2 = (TextView) getActivity().findViewById(R.id.textView2);
//a.setOnClickListener(this);
}
public void showData(String a, String b) {
t1.setText(a);
t2.setText(b);
}
Button b;
EditText e1,e2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_another, container, false);
return rootView;
}
Comm comm;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
comm = (Comm) activity;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
b = (Button) getActivity().findViewById(R.id.buttonA);
e1 = (EditText) getActivity().findViewById(R.id.editText);
e2 = (EditText) getActivity().findViewById(R.id.editText2);
b.setOnClickListener(this);
}
@Override
public void onClick(View view) {
comm.sendData(e1.getText().toString(), e2.getText().toString());
}
public interface Comm {
public void sendData(String a, String b);
}
FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getSupportFragmentManager();
if (savedInstanceState == null) {
fragmentManager.beginTransaction()
.add(R.id.container, new MainFragment())
.commit();
}
}
@Override
public void sendData(String a, String b) {
MainFragment mainFragment = new MainFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container,mainFragment).commit();
mainFragment.showData(a,b);
}
public void newone(View view) {
AnotherFragment anotherFragment = new AnotherFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container,anotherFragment).commit();
}
片段替换导致nullpointerexception。
java.lang.NullPointerException
at com.exa.researchanddev.app.MainFragment.showData(MainFragment.java:31)
at com.exa.researchanddev.app.MainActivity.sendData(MainActivity.java:57)
at com.exa.researchanddev.app.AnotherFragment.onClick(AnotherFragment.java:42)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
而且,......
@Override
public void sendData(String a, String b) {
Toast.makeText(this, a + " = "+ b, Toast.LENGTH_SHORT).show();
MainFragment mainFragment = new MainFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container,mainFragment).commit();
//mainFragment.showData(a,b);
}
当我评论showData函数时,代码工作正常。碎片正在被取代。
未注释时,抛出NullPointerException。
答案 0 :(得分:3)
你的问题在这里:
@Override
public void sendData(String a, String b) {
MainFragment mainFragment = new MainFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container,mainFragment).commit();
mainFragment.showData(a,b);
}
Fragment的事务是异步的。因此,Fragment中的TextViews为null。更好的方法是将Strings作为参数传递给Fragment。
@Override
public void sendData(String a, String b) {
MainFragment mainFragment = new MainFragment();
Bundle bundle = new Bundle();
bundle.putString("A", a);
bundle.putString("B", b);
mainFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.container,mainFragment).commit();
}
现在,您可以阅读碎片onCreateView中的参数数据。
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
a = (Button) getActivity().findViewById(R.id.button);
t1 = (TextView) getActivity().findViewById(R.id.textView);
t2 = (TextView) getActivity().findViewById(R.id.textView2);
if (getArguments() != null) {
t1.setText(getArguments().getString("A"));
t2.setText(getArguments().getString("B"));
}
}
答案 1 :(得分:0)
你的问题在于:
a = (Button) getActivity().findViewById(R.id.button);
...
在此之前调用
return inflater.inflate(R.layout.fragment_another, container, false);
您获得的NPE是因为您尝试从尚未充气的布局中引用视图[R.id.button
]的ID。
在视图充气后尝试移动a = ...
和以下变量赋值。
覆盖onLayout
方法,该方法将在视图膨胀后调用。在此,您应该能够从布局中进行变量赋值。
protected void onLayout(boolean changed, int l, int t, int r, int b){
super.onLayout(changed, l, t, r, b);
...
}