在与片段进行通信时,我得到致命异常。
此应用程序的主要功能是
1)主要活动包含两个片段A&乙
2)片段A包含一个按钮
3)片段B包含一个TextView。
4)如果我点击FrgamentA中的按钮计数器应该增加,这将显示在片段B中
例外如下::
04-29 08:08:29.234: E/AndroidRuntime(1289): FATAL EXCEPTION: main
04-29 08:08:29.234: E/AndroidRuntime(1289): Process: com.example.fragmentscommunication, PID: 1289
04-29 08:08:29.234: E/AndroidRuntime(1289): java.lang.NullPointerException
04-29 08:08:29.234: E/AndroidRuntime(1289): at com.example.fragmentscommunication.FragmentA.onClick(FragmentA.java:37)
04-29 08:08:29.234: E/AndroidRuntime(1289): at android.view.View.performClick(View.java:4438)
04-29 08:08:29.234: E/AndroidRuntime(1289): at android.view.View$PerformClick.run(View.java:18422)
04-29 08:08:29.234: E/AndroidRuntime(1289): at android.os.Handler.handleCallback(Handler.java:733)
04-29 08:08:29.234: E/AndroidRuntime(1289): at android.os.Handler.dispatchMessage(Handler.java:95)
04-29 08:08:29.234: E/AndroidRuntime(1289): at android.os.Looper.loop(Looper.java:136)
04-29 08:08:29.234: E/AndroidRuntime(1289): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-29 08:08:29.234: E/AndroidRuntime(1289): at java.lang.reflect.Method.invokeNative(Native Method)
04-29 08:08:29.234: E/AndroidRuntime(1289): at java.lang.reflect.Method.invoke(Method.java:515)
04-29 08:08:29.234: E/AndroidRuntime(1289): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-29 08:08:29.234: E/AndroidRuntime(1289): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-29 08:08:29.234: E/AndroidRuntime(1289): at dalvik.system.NativeStart.main(Native Method)
我的java代码是
MainActivity.java
public class MainActivity extends ActionBarActivity implements communicator{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void respond(String str) {
// TODO Auto-generated method stub
FragmentManager fm = getFragmentManager();
FragmentB fB = (FragmentB) fm.findFragmentById(R.id.fragment2);
fB.setText(str);
}
}
FragmentA.java
public class FragmentA extends Fragment implements OnClickListener{
Button bt;
int counter = 0;
communicator comm;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_a, container,false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
bt=(Button) getActivity().findViewById(R.id.button1);
bt.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
comm.respond("Button was clicked "+counter +"times");
}
}
FragmentB.java
public class FragmentB extends Fragment{
TextView tv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.fragment_b, container,false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
tv = (TextView) getActivity().findViewById(R.id.textView1);
}
public void setText(String data)
{
tv.setText(data);
}
}
答案 0 :(得分:0)
尝试以下代码: -
public class FragmentA extends Fragment implements OnClickListener{
Button bt;
int counter = 0;
communicator comm;
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view = inflater.inflate(R.layout.fragment_a, container,false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
tv = (TextView) view.findViewById(R.id.textView1);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
comm.respond("Button was clicked "+counter +"times");
}
}
错误,因为您正在使用片段视图而未获取片段视图。