Search code examples
androiduser-interfacefragmentfragmentmanager

Communicating with fragment NullPointerException


I'm very new to Android UI, I've been looking for a place I can learn the basic structure of UI and how communication and layouts should work. But regardless. My problem comes from me trying to change the value of a TextView within a fragment. I keep getting a nullPointerException when i try to change the value. I am using a fragment manager to refer to the fragment.

Here's the fragment code:

package com.archronix.infotainment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class Fragment1 extends android.support.v4.app.Fragment {
public TextView status;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_fragment1, container, 
    false);
    return v;
}

public static Fragment1 newInstance() {
    Fragment1 f = new Fragment1();
    return f;
}

public void postText(String str){
    status.setText(status.getText()+"\n"+str);
}
}

And this is how i declare my fragment manager:

fragment1 = (Fragment1) 
getSupportFragmentManager().findFragmentById(R.id.fragment1);

And, how I use it:

fragment1.postText("testtext");

My logcat:

05-15 10:56:42.406 4109-4109/com.archronix.infotainment E/AndroidRuntime: FATAL EXCEPTION: main Process: com.archronix.infotainment, PID: 4109 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.archronix.infotainment/com.archronix.infotainment.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.archronix.infotainment.Fragment1.postText(java.lang.String)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at android.app.ActivityThread.access$800(ActivityThread.java:144) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.archronix.infotainment.Fragment1.postText(java.lang.String)' on a null object reference at com.archronix.infotainment.MainActivity.onCreate(MainActivity.java:102) at android.app.Activity.performCreate(Activity.java:5933) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)  at android.app.ActivityThread.access$800(ActivityThread.java:144)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5221)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

Apologies for my newbieness. I've read 20 stack overflow posts regarding similar topics but all seem to be either outdated or not consistent. What is the best way to accomplish my task? Thank you.


Solution

  • In onCreateView you should initialize your TextView like so:

    status = (TextView) v.findViewById(R.id.tv_status);
    

    If you know the text that you want to set before the FragmentTransaction you can use a Bundle (How to transfer some data to another Fragment?)