提问人:Rakesh Saini 提问时间:4/18/2022 最后编辑:Rakesh Saini 更新时间:4/18/2022 访问量:383
TextView NullPointerException,在android中使用setText(value)期间获取空对象引用?
TextView NullPointerException, getting null object reference during using setText(value) in android?
问:
我在对 TextView 使用 setText(value) 时遇到错误。我正在使用 dataBinding 绑定布局并从 xml 中查找 id。任何解决方案。我已经启用了数据绑定。 请参阅下面的代码。
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:orientation="horizontal"
android:background="@color/green">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_cart"
android:layout_centerVertical="true"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerInParent="true">
<TextView
android:id="@+id/textTotal"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Total : "
android:textColor="@color/black"
android:textSize="16sp"
android:textStyle="bold" />
<TextView
android:id="@+id/cartValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textTotal"
android:text=""
android:textSize="16sp"
android:textColor="@color/black"
android:textStyle="bold"/>
</LinearLayout>
<TextView
android:id="@+id/textcontinue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/drawable_addtocart_background"
android:padding="8dp"
android:text="Continue"
android:textColor="@color/white"
android:textSize="16sp"
android:textStyle="bold" />
</RelativeLayout>
</layout>
CartActivity 类
package com.example.rangeela_cart.Activity;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.example.rangeela_cart.R;
import com.example.rangeela_cart.databinding.ActivityCartBinding;
public class CartActivity extends AppCompatActivity {
private ActivityCartBinding binding;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_cart);
initView();
}
private void initView() {
// updateCart("50");
}
public void updateCart(Context context, String cartValue) {
Log.e("cart cart value ", cartValue);
try {
if (cartValue != null) {
binding.textTotal.setText(cartValue);
Toast.makeText(context, "Cart Value is not null", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Cart Value is null", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(context, "exception " + e, Toast.LENGTH_LONG).show();
Log.e("Exception ", String.valueOf(e));
}
}
}
适配器类
package com.example.rangeela_cart.Adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.rangeela_cart.Activity.CartActivity;
import com.example.rangeela_cart.R;
import com.example.rangeela_cart.databinding.ItemHomePopularProBinding;
import com.example.rangeela_cart.models.HomePopularProModel;
import java.util.List;
public class HomeFragPopularProAdapter extends RecyclerView.Adapter<HomeFragPopularProAdapter.myViewHolder> {
private Context context;
private List<HomePopularProModel> list;
public HomeFragPopularProAdapter(Context context, List<HomePopularProModel> list) {
this.context = context;
this.list = list;
}
public HomeFragPopularProAdapter(Context context) {
this.context = context;
}
@NonNull
@Override
public myViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
ItemHomePopularProBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.item_home_popular_pro, parent, false);
return new myViewHolder(binding);
}
@Override
public void onBindViewHolder(@NonNull myViewHolder holder, int position) {
HomePopularProModel model = list.get(position);
Glide.with(context).load(model.getPro_image_url()).into(holder.binding.imagePopProduct);
holder.binding.textAvailability.setText(model.getPro_availability());
holder.binding.textCompanyName.setText(model.getPro_company_name());
holder.binding.textPopProductPrice.setText(model.getPro_price());
holder.binding.layoutAddToCart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CartActivity cartActivity = new CartActivity();
cartActivity.updateCart(context, model.getPro_price());
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class myViewHolder extends RecyclerView.ViewHolder {
ItemHomePopularProBinding binding;
public myViewHolder(ItemHomePopularProBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
onItemClickListner onItemClickListner;
public void setOnItemClickListner(HomeFragPopularProAdapter.onItemClickListner onItemClickListner) {
this.onItemClickListner = onItemClickListner;
}
public interface onItemClickListner{
void onClick(String str);//pass your object types.
}
}
我正在从适配器类调用 updateCart() 方法并从那里获取值,我和 cartValue 不为 null。将此 (cartValue) 设置为 TextView-> textTotal 时,会发生 NullPointerException。 错误如下 - >
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
答: 暂无答案
评论
updateCart
CartActivity cartActivity = new CartActivity()