回收程序视图 - 尝试在空对象引用上调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'



我正在创建一个应用程序,它可以让你知道你选择的某些加密货币何时进入你的预算价格范围,让你知道何时购买,对于主活动上的这个应用程序,我需要一个我将通过API获取的所有加密货币的列表,为此我使用了一个回收器视图,我创建了适配的和模型类以及布局,我输入了一些测试值,看看它是否会显示,我得到了这个错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

谢谢。

主活动.java

package com.example.pricepointcrypto;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
CryptoCurrencyAdapter cryptoCurrencyAdapter;
ArrayList<CryptoCurrency> cryptoCurrencies = new ArrayList<>();
private String[] currencyNames = {"Bitcoin (BTC)", "Ethereal (ETH)"};
private String[] currencyRates = {"20509.88 USD" , "1174.3 USD"};
private String[] mktCap = {"3440.0%", "3235.5765%"};
private String[] totalCoins = {"3345444", "3215434"};
private String[] tradedVolume = {"4534B", "98978B"};
private int[] currencyLogos = {R.drawable.cryptocurrencylogo, R.drawable.ic_launcher_background};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int i=0;i<currencyNames.length;i++)
{
CryptoCurrency cryptoCurrency = new CryptoCurrency();
cryptoCurrency.setCurrencyName(currencyNames[i]);
cryptoCurrency.setCurrentRate(currencyRates[i]);
cryptoCurrency.setMarketCap(mktCap[i]);
cryptoCurrency.setTotalCoins(totalCoins[i]);
cryptoCurrency.setTradedVolume(tradedVolume[i]);
cryptoCurrency.setCurrencyLogo(currencyLogos[i]);
cryptoCurrencies.add(cryptoCurrency);
}

cryptoCurrencyAdapter = new CryptoCurrencyAdapter(cryptoCurrencies);
recyclerView = (RecyclerView)findViewById(R.id.crypto_currencies);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(cryptoCurrencyAdapter);
}
}

CryptoCurrency.java

package com.example.pricepointcrypto;
public class CryptoCurrency {
private String currencyName;
private String currentRate;
private String marketCap;
private String totalCoins;
private String tradedVolume;
private int currencyLogo;
public int getCurrencyLogo() {
return currencyLogo;
}
public void setCurrencyLogo(int currencyLogo) {
this.currencyLogo = currencyLogo;
}
public String getCurrencyName() {
return currencyName;
}
public void setCurrencyName(String currencyName) {
this.currencyName = currencyName;
}
public String getCurrentRate() {
return currentRate;
}
public void setCurrentRate(String currentRate) {
this.currentRate = currentRate;
}
public String getMarketCap() {
return marketCap;
}
public void setMarketCap(String marketCap) {
this.marketCap = marketCap;
}
public String getTotalCoins() {
return totalCoins;
}
public void setTotalCoins(String totalCoins) {
this.totalCoins = totalCoins;
}
public String getTradedVolume() {
return tradedVolume;
}
public void setTradedVolume(String tradedVolume) {
this.tradedVolume = tradedVolume;
}
}

CryptoCurrencyAdapter.java

package com.example.pricepointcrypto;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class CryptoCurrencyAdapter extends 
RecyclerView.Adapter<CryptoCurrencyAdapter.ViewHolder> {
List<CryptoCurrency> cryptoCurrencyList;
Context context;
public CryptoCurrencyAdapter(List<CryptoCurrency> cryptoCurrencyList) {
this.cryptoCurrencyList = cryptoCurrencyList;
}
@NonNull
@Override
public CryptoCurrencyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int 
viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.crypto_card_layout, 
parent,false);
ViewHolder viewHolder = new ViewHolder(view);
context = parent.getContext();
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull CryptoCurrencyAdapter.ViewHolder holder, int position) {
CryptoCurrency cryptoCurrency = cryptoCurrencyList.get(position);
holder.currencyName.setText(cryptoCurrency.getCurrencyName());
holder.currentRate.setText(cryptoCurrency.getCurrentRate());
holder.marketCap.setText(cryptoCurrency.getMarketCap());
holder.totalCoins.setText(cryptoCurrency.getTotalCoins());
holder.tradedVolume.setText(cryptoCurrency.getTradedVolume());
holder.currencyImg.setImageResource(cryptoCurrency.getCurrencyLogo());
holder.currencyCard.setOnClickListener((View.OnClickListener) view -> Toast.makeText(context,"The position is:"+position,Toast.LENGTH_SHORT).show());
}
@Override
public int getItemCount() {
return cryptoCurrencyList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView currencyImg;
TextView currencyName;
TextView currentRate;
TextView marketCap;
TextView totalCoins;
TextView tradedVolume;
CardView currencyCard;

public ViewHolder(@NonNull View itemView) {
super(itemView);
currencyImg = itemView.findViewById(R.id.currency_logo);
currencyName = itemView.findViewById(R.id.currency_name);
currentRate = itemView.findViewById(R.id.current_rate);
marketCap = itemView.findViewById(R.id.market_cap);
totalCoins = itemView.findViewById(R.id.coin_total);
tradedVolume = itemView.findViewById(R.id.traded_volume);
currencyCard = itemView.findViewById(R.id.currency_card);
}
}

}

您的代码运行良好。因此,请更改crypto_card_layout。试试看!https://prnt.sc/jkS40DJ6nzZI

<?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="wrap_content"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:id="@+id/currency_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="10dp"
app:cardCornerRadius="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/currency_logo"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_margin="3dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/currency_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:textColor="@color/black"
android:textSize="10dp" />
<TextView
android:id="@+id/current_rate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:textColor="@color/black"
android:textSize="10dp" />
<TextView
android:id="@+id/market_cap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:textColor="@color/black"
android:textSize="10dp" />
<TextView
android:id="@+id/coin_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:textColor="@color/black"
android:textSize="10dp" />
<TextView
android:id="@+id/traded_volume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:textColor="@color/black"
android:textSize="10dp" />
</LinearLayout>

</androidx.cardview.widget.CardView>

</LinearLayout>

相关内容