从另一个视图更改TextView文本

时间:2018-12-21 00:42:24

标签: android xml view

我有一个弹出视图,并且在扩大视图之前,我试图从另一个视图更改值。但是问题是该应用程序崩溃了,并给出了一个似乎根本不相关的错误,但是如果我删除了它可以正常工作的值的“更改”,则再次出现该错误。在这里添加错误将无济于事,因为它实际上是所有其他类的特定内容。

这是我的弹出窗口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>

我正在像这样夸大视图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");

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

1 个答案:

答案 0 :(得分:1)

您应该先更改视图的值,然后再更改其值。您无法更改不存在的值。