-2

im working the last days on a small app but since 2 days i cant set a text to my textview. I know that normally it has to be made in this way:

TextView textview1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textview1 = findViewById(R.id.tvid1);
    textview1.setText("blablabla");
}

In my case is my layout not directly the main layout where the textview is. Im using the default Navigation Drawer Example and their is another layout called that refers to the main-content-layout. I let the program do something in another java-class and that class return a String Value that has to be displayed in my TextView. But I can get data from EditText-Field they are aswell in the same layout.

And this is the Error when my application has to set the text:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

EDIT - 13.05.18 16:00:

when i put TextView calc_price_output into the onCreate methode and the textview set the text. But why he dont do it in another methode that use the same variable :?

PROBLEM SOLVED - But no idea how ... the problem exists only in the last methode. All other works perfectly.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
AkEgo
  • 23
  • 6

4 Answers4

0

you have not typecasted your object calc_price_output to hold a reference of textview reference.

just typecast it like this :-

calc_price_output = (TextView)findViewById(R.id.Tvid);

Edit : I suppose you are correctly calling your function displayOutput() in the onCreate() or any event for this change to show.

bhavesh27
  • 94
  • 7
  • Thanks for your answer. But this wont work aswell. Same error. ||| @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); calc_price_output = (TextView) findViewById(R.id.tv_calculate_price); – AkEgo May 13 '18 at 09:46
  • Then can you please share both your .java and .xml file by editing the question – bhavesh27 May 13 '18 at 09:50
  • done, look at top – AkEgo May 13 '18 at 09:57
  • i have one suggestion just try it may be it will work, just put this line of textview intialiazation at the end of OnCreate i.e. after intialiazation of navigation drawer. – bhavesh27 May 13 '18 at 10:03
  • good idea but dont works aswell :( – AkEgo May 13 '18 at 10:07
  • in displayOutput function use this so that maybe correct instance can be located. i.e. this.calc_price_output.setText(your string here); – bhavesh27 May 13 '18 at 10:10
  • Starting from API 26 you don't need to cast – Suleyman May 13 '18 at 10:13
  • @bhavesh27 when i use this: ||| public void displayOutput(String spritprice){ System.out.println(spritprice); calc_price_output = (TextView) findViewById(R.id.tv_calculate_price); this.calc_price_output.setText(spritprice); } ||| i get this as error ||| java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference – AkEgo May 13 '18 at 10:38
0

you need to call the function from oncreate method

private TextView calc_price_output;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        calc_price_output = findViewById(R.id.tv_calculate_price);
        displayOutput("blablabla"); //call the function
}

//the string returns to this method
public void displayOutput(String spritprice){
        calc_price_output.setText(spritprice);
}
johnson
  • 79
  • 8
  • is there no other way to call it from another class ... because im working in another class and from this class im return to the displayOutput... – AkEgo May 13 '18 at 09:50
0

The answers here suggest that you must call your setText in onCreate. It's not true, you don't have to, you can do it elsewhere, the rule is that you do it after findViewById.

Your Main.java inflates activity_main layout, but the button that you have shown is in act_calculate.xml layout file. Therefore findViewById returns null, either move your button to activity_main or use the include to include it in your main layout.

Suleyman
  • 2,765
  • 2
  • 18
  • 31
-1

You should set text in Content XML file instead of Navigation drawer.like this. e.g:

View view = inflater.inflate(R.layout.fragment_home, container, false);

txtname = view.findViewById(R.id.usersession);

txtname.setText(name);

return view;

M Ali Khattak
  • 27
  • 1
  • 9