gpt4 book ai didi

java - Android Studio - 当 TextView 位于自定义 xml 文件时尝试使用代码更改 TextView 属性时,应用程序崩溃

转载 作者:行者123 更新时间:2023-12-01 19:37:36 25 4
gpt4 key购买 nike

基本上,当我尝试使用 MainActivity 中的代码更改文本字体、颜色或任何内容时,我尝试通过编码更改 TextView 属性,并且此 TextView 位于我创建的名为 popup.xml 的自定义 xml 中。 java 应用程序将崩溃并出现以下错误:java.lang.RuntimeException:无法启动 Activity java.lang.NullPointerException:尝试调用虚拟方法“android.view.Window$Callback android.view.Window.getCallback()”在空对象引用上

来自 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”的内容。

最佳答案

膨胀 View activity_main.xml 不包含任何带有 @+id/textYouLoseTextview

只需将 texview 代码复制/粘贴到 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" />

关于java - Android Studio - 当 TextView 位于自定义 xml 文件时尝试使用代码更改 TextView 属性时,应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59199252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com