我正在用兩個片段寫一個應用程序。第一個片段將文本信息發送到創建這兩個片段的活動。這工作完美。我的第二個片段與第一個片段同時創建。第二個片段在其xml佈局中包含一個textview。我的主要活動應該將基於文本的數據傳遞給第二個片段並顯示文本。問題是當文本到達第二個片段方法時,textview alwasy返回一個空指針。更新片段內Textview時的空指針
//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
'public void setMessage(String newMessage){displayedMessage.setText(newMessage);}' 我已將我的代碼更改爲上述內容。我仍然收到NPE。我注意到'displayedMessage'始終爲空,並且使用'findViewByID()'也返回null – VirtualProdigy
然後,您應該檢查資源ID並確保它存在於剛膨脹的佈局中。 – Barak
我發現我的錯誤。我試圖在第一個片段回調期間調用第二個片段,因爲片段不能直接通信,所以它會拋出NPE。 – VirtualProdigy