0

Basically I am trying to change a TextView properties with coding, and this TextView is located at a custom xml that I have created called popup.xml, when I try to change the text Font, color or anything using code in MainActivity.java the App will crash with the errors: java.lang.RuntimeException: Unable to start activity and java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference

Error from the TextView:

     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.dragoonshimizu.realtest.MainActivity.onCreate(MainActivity.java:31)
        at android.app.Activity.performCreate(Unknown Source:16)
        at android.app.Activity.performCreate(Unknown Source:1)
        at android.app.Instrumentation.callActivityOnCreate(Unknown Source:3)
        at android.app.ActivityThread.performLaunchActivity(Unknown Source:368)

MainActivity.java (Updated by Mike M code) :

package com.example.test;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private PopupWindow popupWindow;
    private LayoutInflater layoutInflater;
    private RelativeLayout relativelayout;

    private TextView textYouLose;
    private TextView countTimeText;

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

        relativelayout = (RelativeLayout) findViewById(R.id.relative);
        textYouLose = (TextView) findViewById(R.id.textYouLose);

        View popupView = getLayoutInflater().inflate(R.layout.popup, null);
        textYouLose = popupView.findViewById(R.id.textYouLose);
        popupView.findViewById(R.id.popupLayout);
        textYouLose.setText("ABC");
        PopupWindow popup = new PopupWindow(popupView);
        popup.setContentView(popupView);


        startCounting();
    }


        public void startCounting() {
            countTimeText = (TextView) findViewById(R.id.countTimeText);
            new CountDownTimer(5000, 1000) {

                public void onTick(long millisUntilFinished) {
                    countTimeText.setText("Time Left: " + millisUntilFinished / 1000);
                }

                // On Finish PopupWindow
                public void onFinish() {
                    // textYouLose.setText("Error Here!");
                    countTimeText.setText("TimeUp!");
                    layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
                    ViewGroup container = (ViewGroup) layoutInflater.inflate(R.layout.popup, null);
                    // Adding true will make it closable if clicked outside window
                    popupWindow = new PopupWindow(container, 1080, 1920, true);
                    popupWindow.showAtLocation(relativelayout, Gravity.NO_GRAVITY, 500, 500);

                    container.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View view, MotionEvent motionEvent) {
                            popupWindow.dismiss();
                            return true;
                        }
                    });
                }
            }.start();

        }

    }

popup.xml :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#E16A6A"
    android:id="@+id/popupLayout"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textYouLose"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layoutDirection="rtl"
        android:text="You lose!"
        android:textAlignment="center"
        android:textSize="40dp" />
</RelativeLayout>

Currently the app doesn't crash with the updated code, but neither it changes the text to what I have set it to "ABC".

UAE Mike
  • 9
  • 5
  • Does this answer your question? [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – coroutineDispatcher Dec 05 '19 at 16:21
  • 1
    The given code should not be throwing that particular Exception. The `NullPointerException` should be happening on the `textYouLose.setText()`. Are you sure that's the stack trace from that code? Did you previously have a `findViewById()` call outside of `onCreate()`? In any case, you cannot modify that `TextView` in `popup` until it's been inflated. You apparently mean to use that for a `PopupWindow`, so don't try to modify `textYouLose` until you're setting up the `PopupWindow`. – Mike M. Dec 05 '19 at 16:25
  • @Mike M. I have updated the error in the post from where it happens from the TextView, I have tried to add the code for changing text font or text or whatever in many different spots and that didn't fix the issue, basically I can't change the textYouLose properties with code at all? – UAE Mike Dec 05 '19 at 16:35
  • OK, that lines up with the given code, now. I just wanted to make sure we're tackling the right issue. Anyhoo, you certainly can modify a `TextView`, once it's been created. That `` element in the layout is not your `TextView` object. That's just a blueprint for `LayoutInflater`, which will read that element, and create a `TextView` object from it. Additionally, that `TextView` does not exist until that layout has been inflated. The only `View`s that exist so far in `onCreate()` are the ones from `activity_main`, because that layout was inflated in the `setContentView()` call. – Mike M. Dec 05 '19 at 16:43
  • If you're going to be using `popup` in that `PopupWindow` you've declared, then you would do your `TextView` modifications on the `View` you inflate for the `PopupWindow`. It will be something like `View popupView = getLayoutInflater().inflate(R.layout.popup, null);`, `textYouLose = popupView.findViewById(R.id.textYouLose);`, `textYouLose.setText("ABC");`, then set `popupView` on the `PopupWindow`. Follow? – Mike M. Dec 05 '19 at 16:44
  • I have added what you typed, and the same error happens `View popupView = getLayoutInflater().inflate(R.layout.popup, null); countTimeText = popupView.findViewById(R.id.textYouLose); popupView.findViewById(R.id.popupLayout); textYouLose.setText("ABC");` Am I missing something? – UAE Mike Dec 05 '19 at 16:53
  • No, sorry, I copied/pasted the wrong `TextView` variable initially. I edited my comment, but you might not have seen it. Replace `countTimeText` with `textYouLose`. – Mike M. Dec 05 '19 at 16:58
  • Yeah, I just did that, now it opens without crashing, but it doesn't change the Text in the popup window. – UAE Mike Dec 05 '19 at 17:00
  • You need to set the `View` you just inflated and modified as the `PopupWindow`'s content. I don't know how you're creating the `PopupWindow` currently, but you can do either `PopupWindow popup = new PopupWindow(popupView);`, or after it's created some other way, `popup.setContentView(popupView);`. – Mike M. Dec 05 '19 at 17:04
  • I think UAE Mike tries to make a popup view. This error occurs of course because the inflated view (`activity_main.xml`) does not contain any textview with id `textYouLose `. In other words, what is `popup.xml` and where is it inflated ? – Thomas Mary Dec 05 '19 at 17:06
  • @Mike M It hasn't worked for me and I might be doing something wrong. Thomas Mary Yes I think you are correct, and I don't know how to inflated. – UAE Mike Dec 05 '19 at 17:15
  • OK, I really don't understand what you're trying to do, now. You said "it doesn't change the Text in the popup window.", but you're not showing the `PopupWindow` anywhere. Are you just trying to change some text in `MainActivity`? If so, what's the purpose of the `popup` layout? – Mike M. Dec 05 '19 at 17:23
  • I have extra code in MainActivity that I thought it wasn't necessary to add, but I will add so you can see the whole code, there is a timer that will make the popup window appears and will show the popup.xml file in the activity_main.xml, but in MainActivity.java I want to change for example the Font of the TextView that is located at popup.xml – UAE Mike Dec 05 '19 at 17:28
  • That's where you need to be modifying the `TextView`. That's a completely different instance of the inflated layout and `PopupWindow` than those you're setting up in `onCreate()`. You can't make changes to one instance, and have them appear in another. Just add `textYouLose = container.findViewById(R.id.textYouLose);`, `textYouLose.setText("ABC");` after the `inflate()` call there. – Mike M. Dec 05 '19 at 17:44

1 Answers1

0

The inflated view activity_main.xml does not contain any Textview with @+id/textYouLose.

Just copy/paste your texview code in your activity_main.xml :

<TextView
        android:id="@+id/textYouLose"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layoutDirection="rtl"
        android:text="You lose!"
        android:textAlignment="center"
        android:textSize="40dp" />
Thomas Mary
  • 1,535
  • 1
  • 13
  • 24