[英]Android Studio Java app crashing when trying to update TextView
[英]Android Studio - App crashing when trying to change a TextView properties with code when it's located at a custom xml file
基本上,我試圖通過編碼更改 TextView 屬性,當我嘗試使用 MainActivity.java 應用程序中的代碼更改文本字體、顏色或任何內容時,此 TextView 位於我創建的名為 popup.xml 的自定義 xml 中將因錯誤而崩潰: java.lang.RuntimeException: Unable to start activity
和java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
來自 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(由 Mike M 代碼更新):
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>
目前,該應用程序不會因更新的代碼而崩潰,但也不會將文本更改為我將其設置為“ABC”的內容。
膨脹的視圖activity_main.xml
不包含任何Textview
與@+id/textYouLose
。
只需在您的activity_main.xml
復制/粘貼您的 texview 代碼:
<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" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.