Richard
Richard

Reputation: 171

Changing the TextView text from another View

I have a Popup View and I am trying to change the values from another view before I inflate the view. But the problem is that the app crashes and gives an error that does not seem to be relevant at all, but then again if I remove the "changing" of the values it works. Adding the error here would not help because it is really specific about all the other classes.

So here is my 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>

And I am inflating the view onclick like this:

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;
            }
        });
    }
});

And this works fine, the popup comes up like it should.
But before that when I call :

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

The whole thing just crashes. Is it possible to give the solution based on this information?

Upvotes: 1

Views: 71

Answers (1)

Master Fathi
Master Fathi

Reputation: 349

You should inflate the view before changing it's values. you can't change the values of something that dosen't exist.

Upvotes: 1

Related Questions