Android getView中的setText提供空指针异常
我看到了一些与这个问题相关的链接,但我已经做了这些链接建议我做的事情。当我开始在适配器中设置值时,出现空指针异常 编辑 现在它不会给出空指针异常,但当它运行时,根本不会显示任何列表。我添加了与之相关的代码。 这是密码Android getView中的setText提供空指针异常,android,baseadapter,android-viewholder,Android,Baseadapter,Android Viewholder,我看到了一些与这个问题相关的链接,但我已经做了这些链接建议我做的事情。当我开始在适配器中设置值时,出现空指针异常 编辑 现在它不会给出空指针异常,但当它运行时,根本不会显示任何列表。我添加了与之相关的代码。 这是密码 public class EventAdapter extends BaseAdapter { Context context; LayoutInflater layoutInflater; // List<InterestAndLanguageBea
public class EventAdapter extends BaseAdapter {
Context context;
LayoutInflater layoutInflater;
// List<InterestAndLanguageBean> interestAndLanguageBeans=new ArrayList<>();
List<ReviewBean>arrayList = new ArrayList<>();
public EventAdapter(Context context, List<ReviewBean> imagesList) {
this.context = context;
this.arrayList = imagesList;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
//LayoutInflater inflater = getLayoutInflater();
convertView = layoutInflater.inflate(R.layout.review_row, parent, false);
name = (TextView) convertView.findViewById(R.id.name);
tv_review_text = (TextView) convertView.findViewById(R.id.tv_review_text);
rb_music = (RatingBar) convertView.findViewById(R.id.rb_music);
rb_food = (RatingBar) convertView.findViewById(R.id.rb_food);
rb_price = (RatingBar) convertView.findViewById(R.id.rb_price);
rb_crowd = (RatingBar) convertView.findViewById(R.id.rb_crowd);
rated = (TextView) convertView.findViewById(R.id.rate);
iv_name = (com.pkmmte.view.CircularImageView) convertView.findViewById(R.id.iv_name);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(reviewBeanArrayList.get(position).getUser_name());
holder.tv_review_text.setText(reviewBeanArrayList.get(position).getComment_text());
holder.rb_crowd.setRating(Float.parseFloat(reviewBeanArrayList.get(position).getComment_crowd_rating()));
holder.rb_music.setRating(Float.parseFloat(reviewBeanArrayList.get(position).getComment_music_rating()));
holder.rb_food.setRating(Float.parseFloat(reviewBeanArrayList.get(position).getComment_food_rating()));
holder.rb_price.setRating(Float.parseFloat(reviewBeanArrayList.get(position).getComment_price_rating()));
Glide.with(ActivityAllReviews.this).load(reviewBeanArrayList.get(position).getUser_image()).placeholder(R.drawable.avatr).into(holder.iv_name);
Float totalRating = (Float.parseFloat(reviewBeanArrayList.get(position).getComment_crowd_rating()) +
Float.parseFloat(reviewBeanArrayList.get(position).getComment_music_rating()) + Float.parseFloat(reviewBeanArrayList.get(position).getComment_food_rating())
+ Float.parseFloat(reviewBeanArrayList.get(position).getComment_price_rating())) / 4;
holder.rated.setText(String.valueOf(totalRating));
return convertView;
}
}
static class ViewHolder {
TextView name, tv_review_text, rated;
com.pkmmte.view.CircularImageView iv_name;
RatingBar rb_food,rb_music,rb_price,rb_crowd;
}
公共类EventAdapter扩展了BaseAdapter{
语境;
LayoutInflater LayoutInflater;
//List interestAndLanguageBeans=new ArrayList();
ListarrayList=新建ArrayList();
公共事件适配器(上下文上下文,列表图像列表){
this.context=上下文;
this.arrayList=imagesList;
layoutInflater=(layoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
}
@凌驾
public int getCount(){
返回arrayList.size();
}
@凌驾
公共对象getItem(int位置){
返回null;
}
@凌驾
公共长getItemId(int位置){
返回0;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
ViewHolder=null;
if(convertView==null){
holder=新的ViewHolder();
//LayoutInflater充气机=getLayoutInflater();
convertView=LayoutFlater.充气(R.layout.review_行,父项,false);
name=(TextView)convertView.findViewById(R.id.name);
tv_review_text=(TextView)convertView.findViewById(R.id.tv_review_text);
rb_music=(评级条)convertView.findViewById(R.id.rb_music);
rb_food=(RatingBar)convertView.findViewById(R.id.rb_food);
rb_价格=(RatingBar)convertView.findViewById(R.id.rb_价格);
rb_群组=(分级栏)convertView.findViewById(R.id.rb_群组);
额定=(TextView)convertView.findViewById(R.id.rate);
iv_name=(com.pkmmte.view.CircularImageView)convertView.findviewbyd(R.id.iv_name);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
holder.name.setText(reviewBeanArrayList.get(position.getUser_name());
holder.tv_review_text.setText(reviewBeanArrayList.get(position.getComment_text());
holder.rb_crowd.setRating(Float.parseFloat(reviewBeanArrayList.get(position.getComment_crowd_rating());
holder.rb_music.setRating(Float.parseFloat(reviewBeanArrayList.get(position.getComment_music_rating());
holder.rb_food.setRating(Float.parseFloat(reviewBeanArrayList.get(position.getComment_food_rating());
holder.rb_price.setRating(Float.parseFloat(reviewBeanArrayList.get(position.getComment_price_rating());
Glide.with(ActivityAllReviews.this).load(reviewBeanArrayList.get(position.getUser_image()).placeholder(R.drawable.avatr).into(holder.iv_name);
Float totalRating=(Float.parseFloat(reviewBeanArrayList.get(position.getComment_crowd_rating())+
Float.parseFloat(reviewBeanArrayList.get(position.getComment_music_rating())+Float.parseFloat(reviewBeanArrayList.get(position.getComment_food_rating())
+parseFloat(reviewBeanArrayList.get(position.getComment\u price\u rating())/4;
holder.rated.setText(String.valueOf(totalRating));
返回视图;
}
}
静态类视窗夹{
文本视图名称、电视评论文本、分级;
com.pkmmte.view.CircularImageView iv_name;
评级栏:rb_食品、rb_音乐、rb_价格、rb_人群;
}
行的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#131313"
android:layout_marginBottom="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.pkmmte.view.CircularImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/avatr"
android:layout_margin="5dp"
android:id="@+id/iv_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Neha Sharma"
android:textSize="10sp"
android:id="@+id/name"
android:textColor="#249D89"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/iv_name"/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/follow"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/name"
android:layout_marginLeft="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Rated"
android:textColor="#ffffff"
android:textSize="10sp"
android:layout_gravity="center"
android:layout_centerVertical="true"
android:id="@+id/tv_rate"
android:layout_toLeftOf="@+id/rate"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4.7"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:textColor="#ffffff"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:background="#3B852C"
android:layout_margin="5dp"
android:textSize="10sp"
android:id="@+id/rate"/>
</RelativeLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:layout_margin="5dp"
android:textColor="#ffffff"
android:text="This is the only place on the entire 2nd floor that plays EDM and Dubstep. Has a typical club interior with a trippy ambience and a crowd that's dancing the night away. Not as packed as a Sutra or
Raas, but definitely a place to stop by......"
android:textSize="10sp"
android:id="@+id/tv_review_text"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="5dp">
<TextView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="Music :"
android:textColor="#484848"
android:layout_marginLeft="20dp"/>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style = "?android:attr/ratingBarStyleSmall"
android:id="@+id/rb_music"/>
<TextView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="Crowd :"
android:textColor="#484848"
android:layout_marginLeft="20dp"/>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style = "?android:attr/ratingBarStyleSmall"
android:id="@+id/rb_crowd"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="5dp">
<TextView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="Food :"
android:textColor="#484848"
android:layout_marginLeft="20dp"/>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style = "?android:attr/ratingBarStyleSmall"
android:id="@+id/rb_food"/>
<TextView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:text="Price :"
android:textColor="#484848"
android:layout_marginLeft="20dp"/>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style = "?android:attr/ratingBarStyleSmall"
android:id="@+id/rb_price"/>
</LinearLayout>
</LinearLayout>
活动xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="@layout/custom_toolbar"/>
<ListView android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="15dp"
android:id="@+id/lin_reviews"
xmlns:android="http://schemas.android.com/apk/res/android" />
</LinearLayout>
活动onCreate方法
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_reviews);
lv_reviews= (ListView) findViewById(R.id.lin_reviews);
try {
reviewBeanArrayList = (ArrayList<ReviewBean>) ActivityAllReviews.this.getIntent().getSerializableExtra("reviewlist");
EventAdapter eventAdapter=new EventAdapter(ActivityAllReviews.this,reviewBeanArrayList);
lv_reviews.setAdapter(eventAdapter);
}catch (NullPointerException e)
{
e.printStackTrace();
}
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u all\u reviews);
lv_reviews=(ListView)findViewById(R.id.lin_reviews);
试一试{
reviewBeanArrayList=(ArrayList)ActivityAllReviews.this.getIntent().getSerializableExtra(“reviewlist”);
EventAdapter EventAdapter=新的EventAdapter(ActivityAllReviews.this,reviewBeanArrayList);
lv_.setAdapter(eventAdapter);
}捕获(NullPointerException e)
{
e、 printStackTrace();
}
}
在我看来,应该是这样的
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.tv_review_text = (TextView) convertView.findViewById(R.id.tv_review_text);
供您编辑
List<ReviewBean>arrayList;//= new ArrayList<>(); the rest is not needed
ListarrayList;//=新的ArrayList();剩下的就不需要了
在我看来,应该是这样的
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.tv_review_text = (TextView) convertView.findViewById(R.id.tv_review_text);
供您编辑
List<ReviewBean>arrayList;//= new ArrayList<>(); the rest is not needed
ListarrayList;//=新的ArrayList();剩下的就不需要了
您可以发布日志吗?reviewBeanArrayList声明在哪里?我看到的是ListarrayList@ZamronyP.Juhara更新的编码你能发布你的日志吗?reviewBeanArrayList声明在哪里?我看到的是ListarrayList@ZamronyP.Juhara更新的代码谢谢。我忘记了这一点,因为之前我使用的是线性布局而不是listview,但是listview没有显示任何内容,我已经检查了我的arraylist是否包含数据出于某种原因,您是否在主活动中更改了alterreviewBeanArrayList
?您在哪一阶段检查了您的库存中是否有物品arraylist@VivekMishrain它显示数据。我调试它,如果reviewBeanArrayList
empty@VivekMishraarraylist不是空的,我检查过了,但你能确认我的布局或初始化是否正确吗?谢谢。我忘记了这一点,因为之前我使用的是线性布局而不是listview,但是listview没有显示任何内容,我已经检查了我的arraylist是否包含数据出于某种原因,您是否在主活动中更改了alterreviewBeanArrayList
?您在哪一阶段检查了您的库存中是否有物品arraylist@VivekMishrain它显示数据。我调试它,并在设置适配器if