1

I'm writting an application with two fragments. The First fragment sends text information to the activity that created both fragments. That work perfectly. My second fragment is created at the same time as the first fragment. The second fragment contains a textview in its xml layout. My main activity should pass the text based data to the second fragment and the text should be displayed. The problem is when the text reachs the second fragments method the textview alwasy returns a null pointer.

//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);
    }
}

Stack Trace:

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
4

2 回答 2

0

Why are you trying to re-set the displayedMessage var? You set it in onCreateView and you have it as a class variable, so it should still be there when you call the serMessage method.

Just drop the findViewById in the setMesage method and I believe your NPE will go away.

于 2012-12-26T00:29:16.503 回答
0

I found my error. I attempted to call the second fragment during the first fragments callback which throw the NPE as fragments can not directly communicate.

于 2012-12-27T01:41:10.553 回答