android - 从另一个 View 更改 TextView 文本

标签 android xml view

我有一个弹出 View ,我试图在膨胀 View 之前更改另一个 View 的值。但问题是应用程序崩溃并给出了一个似乎根本不相关的错误,但是如果我删除它工作的值的“更改”又一次。在此处添加错误无济于事,因为它确实针对所有其他类。

所以这是我的 Popup XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id = "@+id/more_info_popup_window"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#62def8">


    <android.support.constraint.ConstraintLayout
        android:id="@+id/user_gray_score_layout"
        android:layout_width="wrap_content"
        android:layout_height="43dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_marginStart="8dp"
        android:clickable="true"
        android:foreground="?android:attr/selectableItemBackground"
        android:visibility="visible"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/gray_user_star"
            android:layout_width="33dp"
            android:layout_height="24dp"
            android:layout_gravity="left"
            android:layout_marginStart="12dp"
            android:layout_marginTop="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/silver_star_selected" />

        <TextView
            android:id="@+id/gray_star_points"
            android:layout_width="wrap_content"
            android:layout_height="24dp"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:text="7777"
            android:textColor="@color/main_app_color"
            android:textSize="19sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.034"
            app:layout_constraintStart_toEndOf="@+id/gray_user_star"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

我正在像这样膨胀 View onclick:

userScoreConstraintLayout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        int[] score = PostNewActivity.getScore();

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
        View popupView = Objects.requireNonNull(inflater).inflate(R.layout.more_info_popup_window, null);

        // create the popup window
        int width = LinearLayout.LayoutParams.WRAP_CONTENT;
        int height = LinearLayout.LayoutParams.WRAP_CONTENT;
        boolean focusable = true; // lets taps outside the popup also dismiss it
        final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);

        // show the popup window
        // which view you pass in doesn't matter, it is only used for the window tolken
        popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);

        // dismiss the popup window when touched
        popupView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                popupWindow.dismiss();
                return true;
            }
        });
    }
});

这很好用,弹出窗口如期出现。
但在那之前,当我打电话时:

((TextView) view.findViewById(R.id.gray_star_points)).setText("23");

整个事情都崩溃了。是否可以根据这些信息给出解决方案?

最佳答案

您应该在更改 View 值之前膨胀 View 。您无法更改不存在的值。

关于android - 从另一个 View 更改 TextView 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53877868/

相关文章:

android - Gradle构建卡在app:tranformDexArchiveWithDexMergerForDebug

ruby-on-rails - rails - to_xml 将值放在 xml 属性而不是标签中

java - 在 Java 中从 XML 中提取数据

java - 将进度条添加到我的 dataCall 函数 - Android

java - 在 Android 上的 dalvik 中动态类重新加载

c# - Ascii 到 XML 字符集转换

android - 不使用 invalidate(Rect) 只更新 View 的一部分

java - NetBeans 在设计 View 中删除自动生成的 Action 监听器

Android:在任务 Activity 上覆盖窗口

android - 如何为其他 Activity 创建多个 startActivityForResult?