为什么来自不同布局文件的 textView 不能正确 运行 setText 但来自 activity_main 的 textView 可以?

Why won't a textView from a different layout file not properly run setText but textViews from activity_main will?

该程序应该运行 getCurrentLocation,然后将textView“坐标”设置为调用坐标的值。该文本视图是分页视图的一部分,它从一个单独的 .xml 文件中绘制,而不是主布局“activity_main”。

public class MainActivity extends AppCompatActivity
{

    CardAdapter CardAdapter;
    String coordinatesFinal;
    FusedLocationProviderClient fusedLocationProviderClient;
    TextView coordinates;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(MainActivity.this);

        if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)
        {
            getCurrentLocation();
        }else
            {
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},100);
            }

        setupCardItems();

        ViewPager2 CardViewPager = findViewById(R.id.cardViewPager);
        CardViewPager.setAdapter(CardAdapter);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
    {
        if (requestCode == 100 & grantResults.length > 0 && (grantResults[0] + grantResults[1]) == PackageManager.PERMISSION_GRANTED)
        {
            getCurrentLocation();
        }else
            {
                Toast.makeText(getApplicationContext(), "Permission denied.",Toast.LENGTH_SHORT).show();
            }
    }

    @SuppressLint("MissingPermission")
        public void getCurrentLocation()
        {
            coordinates = findViewById(R.id.coordinates);

            LocationManager locationManager = (LocationManager) getSystemService
                    (
                            Context.LOCATION_SERVICE
                    );
            if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
            {
                fusedLocationProviderClient.getLastLocation().addOnCompleteListener(task ->
                {
                    Location location = task.getResult();
                    if (location != null)
                    {
                        coordinates.setText(location.getLatitude()+", "+location.getLongitude());
                    }else
                        {
                            LocationRequest locationRequest = new LocationRequest().setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY).setInterval(10000).setFastestInterval(1000).setNumUpdates(1);
                            LocationCallback locationCallback = new LocationCallback()
                            {
                                @Override
                                public void onLocationResult(LocationResult locationResult)
                                {
                                    Location location1 = locationResult.getLastLocation();
                                    coordinatesFinal = location1.getLatitude()+", "+location1.getLongitude();
                                }
                            };

                            fusedLocationProviderClient.requestLocationUpdates(locationRequest,locationCallback, Looper.myLooper());
                        }
                });
            }else
                {
                    startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                }
        }


    //card view
    private void setupCardItems()
    {
        List<CardItem> CardItems = new ArrayList<>();

        CardItem itemPageOne = new CardItem();
        itemPageOne.setLocation("Home");
        itemPageOne.setTemp("60°f");
        itemPageOne.setWeatherDesc("Cloudy");
        itemPageOne.setWeatherIcon(R.drawable.cloud);
        itemPageOne.setHum("75%");
        itemPageOne.setVis("10");
        itemPageOne.setPrecip("50%");
        itemPageOne.setDew("40°f");
        itemPageOne.setCloud("80%");
        itemPageOne.setFog("75%");

        CardItem itemPageTwo = new CardItem();
        itemPageTwo.setLocation("Madison");
        itemPageTwo.setTemp("60°f");
        itemPageTwo.setWeatherDesc("Sunny");
        itemPageTwo.setWeatherIcon(R.drawable.sun);
        itemPageTwo.setHum("75%");
        itemPageTwo.setVis("10");
        itemPageTwo.setPrecip("50%");
        itemPageTwo.setDew("40°f");
        itemPageTwo.setCloud("80%");
        itemPageTwo.setFog("75%");

        CardItem itemPageThree = new CardItem();
        itemPageThree.setLocation("Milwaukee");
        itemPageThree.setTemp("60°f");
        itemPageThree.setWeatherDesc("Rainy");
        itemPageThree.setWeatherIcon(R.drawable.rain);
        itemPageThree.setHum("75%");
        itemPageThree.setVis("10");
        itemPageThree.setPrecip("50%");
        itemPageThree.setDew("40°f");
        itemPageThree.setCloud("80%");
        itemPageThree.setFog("75%");

        CardItems.add(itemPageOne);
        CardItems.add(itemPageTwo);
        CardItems.add(itemPageThree);

        CardAdapter = new CardAdapter(CardItems);
    }
}

我已将其缩小到无法正常工作的行:

coordinates.setText(location.getLatitude()+", "+location.getLongitude());

我已经测试了与上面完全相同的行,只是使用 activity main 内部的 textView,效果很好。非常感谢任何帮助。

编辑:文件确实崩溃了,我误说没有,我都添加了。xml 文件:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#566D8F"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/topBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:paddingTop="15dp"
        android:paddingBottom="15dp">

        <ImageButton
            android:id="@+id/settingsBar2"
            android:layout_width="27sp"
            android:layout_height="27sp"
            android:layout_gravity="left"
            android:layout_marginStart="20sp"
            android:layout_marginEnd="70sp"
            android:background="@color/background"
            android:cropToPadding="true"
            android:scaleType="fitCenter" />

        <TextView
            android:id="@+id/homeName"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:fontFamily="sans-serif-condensed-light"
            android:gravity="center_horizontal"
            android:maxLines="1"
            android:text="@string/app_name"
            android:textAlignment="center"
            android:textAllCaps="false"
            android:textColor="#FFFFFF"
            android:textSize="20sp"
            android:typeface="normal" />

        <ImageButton
            android:id="@+id/settingsBar"
            android:layout_width="27sp"
            android:layout_height="27sp"
            android:layout_gravity="right"
            android:layout_marginStart="70sp"
            android:layout_marginEnd="20sp"
            android:background="@drawable/settings_handle"
            android:cropToPadding="true"
            android:scaleType="fitCenter" />

    </LinearLayout>

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/cardViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toBottomOf="@+id/topBar" />

</androidx.constraintlayout.widget.ConstraintLayout>

item_container_card.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="20dp">

    <androidx.cardview.widget.CardView
        android:id="@+id/pagerCard"
        android:layout_width="match_parent"
        android:layout_height="440dp"
        android:layout_margin="20dp"
        app:cardBackgroundColor="#EAC0A0"
        app:cardCornerRadius="30dp"
        app:cardElevation="20dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:orientation="vertical">

            <TextView
                android:id="@+id/locationName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-condensed-medium"
                android:gravity="center"
                android:textColor="@color/white"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/coordinates"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-condensed-light"
                android:gravity="center"
                android:textColor="@color/white"
                android:textSize="12sp" />

            <TextView
                android:id="@+id/temperature"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-black"
                android:gravity="center"
                android:textColor="@color/white"
                android:textSize="50sp" />

            <TextView
                android:id="@+id/weatherDesc"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fontFamily="sans-serif-condensed-light"
                android:gravity="center"
                android:textColor="@color/white"
                android:textSize="12sp" />

            <ImageView
                android:id="@+id/imageOnboarding"
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:layout_margin="15dp"
                android:adjustViewBounds="true"
                android:contentDescription="@string/app_name" />

            <TableLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:stretchColumns="1">

                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_horizontal">

                    <TextView
                        android:id="@+id/humidity"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="fill_horizontal"
                        android:fontFamily="sans-serif-condensed-light"
                        android:paddingLeft="30sp"
                        android:paddingBottom="5sp"
                        android:text="Humidity"
                        android:textColor="#FFFFFF"
                        android:textSize="18sp" />

                    <TextView
                        android:id="@+id/humVal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="fill_horizontal"
                        android:fontFamily="sans-serif-condensed-medium"
                        android:paddingStart="10sp"
                        android:paddingEnd="15sp"
                        android:paddingBottom="5sp"
                        android:textColor="#FFFFFF"
                        android:textSize="18sp" />

                    <TextView
                        android:id="@+id/visibility"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="sans-serif-condensed-light"
                        android:paddingStart="15sp"
                        android:paddingEnd="10sp"
                        android:paddingBottom="5sp"
                        android:text="Visibility"
                        android:textColor="#FFFFFF"
                        android:textSize="18sp" />

                    <TextView
                        android:id="@+id/visVal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="fill_horizontal"
                        android:fontFamily="sans-serif-condensed-medium"
                        android:paddingStart="10sp"
                        android:paddingRight="30sp"
                        android:paddingBottom="5sp"
                        android:textColor="#FFFFFF"
                        android:textSize="18sp" />

                </TableRow>

                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_horizontal">

                    <TextView
                        android:id="@+id/precipitation"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="fill_horizontal"
                        android:fontFamily="sans-serif-condensed-light"
                        android:paddingLeft="30sp"
                        android:paddingTop="5sp"
                        android:paddingBottom="5sp"
                        android:text="Precip. "
                        android:textColor="#FFFFFF"
                        android:textSize="18sp" />

                    <TextView
                        android:id="@+id/precipVal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="fill_horizontal"
                        android:fontFamily="sans-serif-condensed-medium"
                        android:paddingStart="10sp"
                        android:paddingTop="5sp"
                        android:paddingEnd="15sp"
                        android:paddingBottom="5sp"
                        android:textColor="#FFFFFF"
                        android:textSize="18sp" />

                    <TextView
                        android:id="@+id/dewPoint"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="sans-serif-condensed-light"
                        android:paddingStart="15sp"
                        android:paddingTop="5sp"
                        android:paddingBottom="5sp"
                        android:text="Dew Point"
                        android:textColor="#FFFFFF"
                        android:textSize="18sp" />

                    <TextView
                        android:id="@+id/dewVal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="fill_horizontal"
                        android:fontFamily="sans-serif-condensed-medium"
                        android:paddingStart="10sp"
                        android:paddingTop="5sp"
                        android:paddingRight="30sp"
                        android:paddingBottom="5sp"
                        android:textColor="#FFFFFF"
                        android:textSize="18sp" />

                </TableRow>

                <TableRow
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_horizontal">

                    <TextView
                        android:id="@+id/cloud"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="fill_horizontal"
                        android:fontFamily="sans-serif-condensed-light"
                        android:paddingLeft="30sp"
                        android:paddingTop="5sp"
                        android:paddingBottom="5sp"
                        android:text="Cloud"
                        android:textColor="#FFFFFF"
                        android:textSize="18sp" />

                    <TextView
                        android:id="@+id/cloudVal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="fill_horizontal"
                        android:fontFamily="sans-serif-condensed-medium"
                        android:paddingStart="10sp"
                        android:paddingTop="5sp"
                        android:paddingEnd="15sp"
                        android:paddingBottom="5sp"
                        android:textColor="#FFFFFF"
                        android:textSize="18sp" />

                    <TextView
                        android:id="@+id/fog"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:fontFamily="sans-serif-condensed-light"
                        android:paddingStart="15sp"
                        android:paddingTop="5sp"
                        android:paddingBottom="5sp"
                        android:text="Fog"
                        android:textColor="#FFFFFF"
                        android:textSize="18sp" />

                    <TextView
                        android:id="@+id/fogVal"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="fill_horizontal"
                        android:fontFamily="sans-serif-condensed-medium"
                        android:paddingStart="10sp"
                        android:paddingTop="5sp"
                        android:paddingEnd="30sp"
                        android:paddingBottom="5sp"
                        android:textColor="#FFFFFF"
                        android:textSize="18sp" />

                </TableRow>

            </TableLayout>

        </LinearLayout>

    </androidx.cardview.widget.CardView>


</LinearLayout>

编辑 2:CardAdapter.java 和 CardItem.java

CardAdapter.java:

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.cardViewHolder>
{
    private List<CardItem> CardItems;

    public CardAdapter(List<CardItem> CardItems) {
        this.CardItems = CardItems;
    }

    @NonNull
    @Override
    public cardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new cardViewHolder(
                LayoutInflater.from(parent.getContext()).inflate(
                        R.layout.item_container_card, parent, false
                )
        );
    }

    @Override
    public void onBindViewHolder(@NonNull cardViewHolder holder, int position) {
        holder.setCardData(CardItems.get(position));
    }

    @Override
    public int getItemCount() {
        return CardItems.size();
    }

    class cardViewHolder extends RecyclerView.ViewHolder
    {
        private TextView locationText;
        private TextView weatherDesc;
        private ImageView weatherIcon;
        private TextView tempVal;
        private TextView humVal;
        private TextView visVal;
        private TextView precipVal;
        private TextView dewVal;
        private TextView cloudVal;
        private TextView fogVal;

        public cardViewHolder(@NonNull View itemView)
        {
            super(itemView);
            locationText = itemView.findViewById(R.id.locationName);
            weatherDesc = itemView.findViewById(R.id.weatherDesc);
            weatherIcon = itemView.findViewById(R.id.imageOnboarding);
            tempVal = itemView.findViewById(R.id.temperature);
            humVal = itemView.findViewById(R.id.humVal);
            visVal = itemView.findViewById(R.id.visVal);
            precipVal = itemView.findViewById(R.id.precipVal);
            dewVal = itemView.findViewById(R.id.dewVal);
            cloudVal = itemView.findViewById(R.id.cloudVal);
            fogVal = itemView.findViewById(R.id.fogVal);

        }

        void setCardData(CardItem CardItem)
        {
            locationText.setText(CardItem.getLocation());
            weatherDesc.setText(CardItem.getWeatherDesc());
            weatherIcon.setImageResource(CardItem.getWeatherIcon());
            tempVal.setText(CardItem.getTemp());
            humVal.setText(CardItem.getHum());
            visVal.setText(CardItem.getVis());
            precipVal.setText(CardItem.getPrecip());
            dewVal.setText(CardItem.getDew());
            cloudVal.setText(CardItem.getCloud());
            fogVal.setText(CardItem.getFog());
        }

    }
}

CardItem.java:

package com.example.viewpagertest;

public class CardItem
{
    private int weatherIcon;
    private String location;
    private String temp;
    private String weatherDesc;
    private String hum;
    private String vis;
    private String precip;
    private String dew;
    private String cloud;
    private String fog;


    //weather icon
    public int getWeatherIcon() {
        return weatherIcon;
    }

    public void setWeatherIcon(int weatherIcon) {
        this.weatherIcon = weatherIcon;
    }

    //location title
    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }
    
    //temperature value
    public String getTemp() {
        return temp;
    }

    public void setTemp(String temp) {
        this.temp = temp;
    }

    //weather description
    public String getWeatherDesc() {
        return weatherDesc;
    }

    public void setWeatherDesc(String weatherDesc) {
        this.weatherDesc = weatherDesc;
    }

    //humidity value
    public String getHum() {
        return hum;
    }

    public void setHum(String hum) {
        this.hum = hum;
    }

    //visibility value
    public String getVis() {
        return vis;
    }

    public void setVis(String vis) {
        this.vis = vis;
    }

    //precipitation value
    public String getPrecip() {
        return precip;
    }

    public void setPrecip(String precip) {
        this.precip = precip;
    }

    //dew point value
    public String getDew() {
        return dew;
    }

    public void setDew(String dew) {
        this.dew = dew;
    }

    //cloud value
    public String getCloud() {
        return cloud;
    }

    public void setCloud(String cloud) {
        this.cloud = cloud;
    }

    //fog value
    public String getFog() {
        return fog;
    }

    public void setFog(String fog) {
        this.fog = fog;
    }
}

这是 运行调试器断点在

时的控制台
coordinates.setText(location.getLatitude()+", "+location.getLongitude());

整个控制台打破了字符限制,所以这是一个包含控制台文本的文档。

Console Text

                if (location != null)
                {
                    // coordinates.setText(location.getLatitude()+", "+location.getLongitude());
                    cardAdapter.setCoordinates(0 /*the cardItem number you want to update*/, location.getLatitude()+", "+location.getLongitude());
                }    

由于坐标 TextView 在您的项目中 xml,您将无法从 MainActivity 的根目录中找到它,而是从您的项目中找到它。


更新
感谢您添加适配器代码:

最简单的方法是在您的适配器中创建一个方法,如下所示:

setCoordinates(int position, String coords) {
    holders.get(position).itemView.findViewById(R.id.coordinates).setText(coords);
}

为了能够在运行时访问持有者,添加以下内容:

public class CardAdapter extends RecyclerView.Adapter<CardAdapter.cardViewHolder>
{
    private List<CardItem> cardItems;
    private List<CardViewHolder> holders;
    ....

    @Override
    public cardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        CardViewHolder holder = new cardViewHolder(
            LayoutInflater.from(parent.getContext()).inflate(
                    R.layout.item_container_card, parent, false
            )
        );
        holders.add(holder);
        return holder;
    }

另一种选择是将字段 coordinates 添加到您的 CardItem 并编写 setCoordinates 方法,如下所示:

setCoordinates(int position, String coords) {
    cardItems.get(position).setCoordinates(coords);
    notifyDataSetChanged();  // to reload the holders, only notifying the specific position will be faster to reload only the changed holder
}

您需要通过在 CardAdapter 中创建一个方法并从主 activity 调用它来更新适配器项,如下所示 -

在您的适配器中创建此方法class

public void updateAdapter(List<CardItem> CardItems) {
    this.CardItems = CardItems;
    notifyDataSetChanged();
}

替换下行

CardAdapter = new CardAdapter(CardItems);

有了这个

CardAdapter.updateAdapter(CardItems);