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".