-4

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.

  • I have read the post. And i understand how NullPointerExceptions work, my problem is i cant seem to identify where and with what variable the problem occurs in my code. – Arin Yaldizciyan May 15 '17 at 15:20
  • You can use the debugger for that – Daniele May 15 '17 at 15:22
  • As you can see, the debugger tells me that it is on the call to the method from. I do not know why that calls a NullPointerException. I wish I understood android methods well enough to be able to trace my problem but sadly I do not. I'm sorry if there is a solution available, I'm not sure what else to search for. – Arin Yaldizciyan May 15 '17 at 15:27
  • That's not the debugger. have a look here https://developer.android.com/studio/debug/index.html – Daniele May 15 '17 at 15:29
  • Thank you, I will look into it. – Arin Yaldizciyan May 15 '17 at 15:38

2 Answers2

1

status variable is not assigned to any textview. Add the following code in onCreateView before return statement

status=v.findViewById(R.id.<nameoftextview>);
kunwar97
  • 655
  • 6
  • 13
0

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?)

Community
  • 1
  • 1
Rene Ferrari
  • 3,446
  • 3
  • 19
  • 26