首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >回收器视图-尝试在空对象引用上调用虚拟方法的空android.widget.TextView.setText(java.lang.CharSequence)‘

回收器视图-尝试在空对象引用上调用虚拟方法的空android.widget.TextView.setText(java.lang.CharSequence)‘
EN

Stack Overflow用户
提问于 2022-07-15 07:30:25
回答 1查看 65关注 0票数 -2

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

代码语言:javascript
复制
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

谢谢。

Main Acitivty.java

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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);
    }
}

}

EN

回答 1

Stack Overflow用户

发布于 2022-07-15 09:55:25

你的代码运行得很好。所以改变crypto_card_layout吧。试试这个,检查一下!https://prnt.sc/jkS40DJ6nzZI

代码语言:javascript
复制
<?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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72990485

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档