我正在用两个片段写一个应用程序。 First片段将文本信息发送到创建两个片段的活动。这工作完美。我的第二个片段与第一个片段同时创建。第二个片段在其xml布局中包含textview。我的主要活动应该将基于文本的数据传递给第二个片段,并且应该显示文本。问题是当文本到达第二个片段方法时,textview alwasy返回一个空指针。
//This is the main methods creation of the fragment
try {
//message frag on bottom
FragmentManager fM = getFragmentManager();
FragmentTransaction fTrans = fM.beginTransaction();
messFrag = new DisplayMessageFragment();
fTrans.add(R.id.BottomDisplayFragment, messFrag,"MESSAGE_FRAGMENT");
fTrans.commit();
} catch(Exception e) {
e.printStackTrace();
}
//this is whe main methods call the fragments text view updating method
@Override
public void getDisplayWindowMessage_StartFrag(String message) {
getFragmentManager().findFragmentByTag("MESSAGE_FRAGMENT");
messFrag.setMessage(message);
}
//This is my fragment class
public class DisplayMessageFragment extends Fragment {
TextView displayedMessage;
View view ;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
view = inflater.inflate(R.layout.displaymess_frag,container, false);
displayedMessage = (TextView) view.findViewById(R.id.MessageDisplay);
return view;
}
public void setMessage(String newMessage){
//displayedMessage=(TextView) view.findViewById(R.id.MessageDisplay);
displayedMessage = (TextView) getView().findViewById(R.id.MessageDisplay);
displayedMessage.setText(newMessage);
}
}
堆栈追踪:
java.lang.NullPointerException
12-26 02:06:14.505: E/AndroidRuntime(710): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
12-26 02:06:14.505: E/AndroidRuntime(710): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
12-26 02:06:14.505: E/AndroidRuntime(710): at android.app.ActivityThread.access$600(ActivityThread.java:123)
12-26 02:06:14.505: E/AndroidRuntime(710): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
12-26 02:06:14.505: E/AndroidRuntime(710): at android.os.Handler.dispatchMessage(Handler.java:99)
12-26 02:06:14.505: E/AndroidRuntime(710): at android.os.Looper.loop(Looper.java:137)
12-26 02:06:14.505: E/AndroidRuntime(710): at android.app.ActivityThread.main(ActivityThread.java:4424)
12-26 02:06:14.505: E/AndroidRuntime(710): at java.lang.reflect.Method.invokeNative(Native Method)
12-26 02:06:14.505: E/AndroidRuntime(710): at java.lang.reflect.Method.invoke(Method.java:511)
12-26 02:06:14.505: E/AndroidRuntime(710): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
12-26 02:06:14.505: E/AndroidRuntime(710): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
12-26 02:06:14.505: E/AndroidRuntime(710): at dalvik.system.NativeStart.main(Native Method)
12-26 02:06:14.505: E/AndroidRuntime(710): Caused by: java.lang.NullPointerException
12-26 02:06:14.505: E/AndroidRuntime(710): at .DisplayMessageFragment.setMessage(DisplayMessageFragment.java:38)
12-26 02:06:14.505: E/AndroidRuntime(710): at .Schduler_Activity.getDisplayWindowMessage_StartFrag(Schduler_Activity.java:79)
12-26 02:06:14.505: E/AndroidRuntime(710): at
StartFragment.onCreateView(StartFragment.java:31)
12-26 02:06:14.505: E/AndroidRuntime(710): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:828)
12-26 02:06:14.505: E/AndroidRuntime(710): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
12-26 02:06:14.505: E/AndroidRuntime(710): at android.app.BackStackRecord.run(BackStackRecord.java:622)
12-26 02:06:14.505: E/AndroidRuntime(710): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
12-26 02:06:14.505: E/AndroidRuntime(710): at android.app.Activity.performStart(Activity.java:4474)
12-26 02:06:14.505: E/AndroidRuntime(710): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1929)
12-26 02:06:14.505: E/AndroidRuntime(710): ... 11 more
答案 0 :(得分:0)
您为什么要重新设置displayedMessage
var?你在onCreateView
中设置它并将它作为一个类变量,所以当你调用serMessage
方法时它应该仍然存在。
只需将findViewById
放入setMesage
方法即可,我相信您的NPE会消失。
答案 1 :(得分:0)
我发现了我的错误。我尝试在第一个片段回调期间调用第二个片段,因为片段无法直接通信而抛出NPE。