简体   繁体   English

Recycler View - 尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)”

[英]Recycler View - Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

I'm creating a app that lets you know when certain crypto currencies that you select enter your budget range in terms of price letting you know when its time to buy, for this app on the main acitivty i need a list of all the crypto currencies that i will fetch via API and for that i am using a recycler view i have created the adapted and the model class as well as the layout and i have entered a few test values to see if it would display and i get this error:我正在创建一个应用程序,让您知道您选择的某些加密货币何时进入您的预算范围,让您知道何时购买,对于主要活动上的这个应用程序,我需要所有加密货币的列表我将通过 API 获取,为此我正在使用回收器视图,我已经创建了适配和模型类以及布局,并且我输入了一些测试值以查看它是否会显示并且我收到此错误:

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

Thank You.谢谢你。

Main Acitivty.java主要活动.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加密货币.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 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);
    }
}

} }

Your code is working perfectly.您的代码运行良好。 So change crypto_card_layout.所以改变crypto_card_layout。 Try this and check!试试这个并检查! https://prnt.sc/jkS40DJ6nzZI 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Recycler view android studioAttempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference - Recycler view android studioAttempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 如何解决对空对象引用调用虚拟方法&#39;void android.widget.TextView.setText(java.lang.CharSequence)&#39;的尝试 - How to resolve Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference (RecyclerView) 尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)” - (RecyclerView) Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference Java错误:尝试在空对象引用上调用虚拟方法&#39;void android.widget.TextView.setText(java.lang.CharSequence)&#39; - Java Error: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 可能重复:尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)” - May be duplicated : Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference RecyclerView:尝试在 null object 引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)” - RecyclerView: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 在andoid应用程序上出现错误'尝试调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)' - Getting error 'Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' ON andoid app 在Home.onCreate上的空对象引用上无效android.widget.TextView.setText(java.lang.CharSequence)&#39; - void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at Home.onCreate 在空对象引用上使 android.widget.TextView.setText(java.lang.CharSequence)&#39; 无效 - Void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference 尝试在空对象引用上调用虚拟方法&#39;void android.widget.EditText.setText(java.lang.CharSequence)&#39; - Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM