gpt4 book ai didi

java - 自定义 ListView 中的空指针异常 View 持有者

转载 作者:行者123 更新时间:2023-12-02 10:34:27 25 4
gpt4 key购买 nike

package com.example.ahmadal_bargouthy.project1;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import static android.app.PendingIntent.getActivity;

public class MainActivity extends AppCompatActivity {


int[] IMAGES = {R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5 ,R.drawable.img6,R.drawable.img7,R.drawable.img8,R.drawable.img9,R.drawable.img10};

String[] NAMES = {"Lighter Watch","MINI TRACKER ANTI-LOST","FUNNY LION WIG","ROCKET LAUNCHER",
"NEW SMART WATCH","LINE REWIND","LUMINOUS TEMPERED GLASS ","E58 DRONE WIFI","LED POWER MASK","PAINT SKULL 3D PRINTED HOODIES"};

String[] DESC = {"Flameless windproof","SMART BLUETOOTH 4.0 TRACER GPS LOCATOR","FUNNY LION WIG HEAD CAP HAT WITH EAR",
"ROCKET LAUNCHER DEFAULT GIRL SKIN DEFAULT SKIN FORTNITE","MEN WOMEN HEART RATE MONITOR BLOOD PRESSURE FITNESS TRACKER",
"LINE REWIND 24K GOLD SERUM","CASE FOR IPHONE","FPV ANGLE HD CAMERA HIGH HOLD MODE"," "," "};

String[] PRICE = {"20","15","12.95","25.98","27.98","23.99","14.95","24.95","38.18"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ListView listView = (ListView)findViewById(R.id.productList);

CustomAdapter customAdapter = new CustomAdapter(this,NAMES,DESC,PRICE,IMAGES);

listView.setAdapter(customAdapter);

Intent i = getIntent();
String userName = i.getStringExtra("User_Name");
Toast toast = Toast.makeText(this,"Hello " + userName,Toast.LENGTH_LONG);
toast.show();
setTitle("Welcome " + userName);
}

class CustomAdapter extends ArrayAdapter<String>{

private String[] NAMES;
private String[] DESC;
private String[] PRICE;
private int[] IMAGES;

private Activity context;

public CustomAdapter(Activity context, String[]NAMES, String[] DESC , String[] PRICE, int[] IMAGES) {
super(context, R.layout.customlayout,NAMES);

this.context = context;

this.NAMES = NAMES;
this.DESC = DESC;
this.PRICE = PRICE;
this.IMAGES = IMAGES;
}

@androidx.annotation.NonNull
@Override
public View getView(int position, @androidx.annotation.Nullable View convertView, @androidx.annotation.NonNull ViewGroup parent) {
View r=convertView;

ViewHolder viewHolder = null;

if (r==null){
LayoutInflater layoutInflater = context.getLayoutInflater();
r=layoutInflater.inflate(R.layout.customlayout,null,true);
r.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)r.getTag();

}

ViewHolder vm = new ViewHolder();

//vm.imageView.setImageResource(IMAGES[position]);

int x = R.drawable.ic_launcher_background;

viewHolder.imageView.setImageResource(x);
viewHolder.nameTxt.setText(NAMES[position]);
viewHolder.descTxt.setText(DESC[position]);
viewHolder.priceTxt.setText(PRICE[position]);



return r;
}

class ViewHolder{
TextView nameTxt = (TextView)findViewById(R.id.Name);
TextView descTxt = (TextView)findViewById(R.id.Desc);
TextView priceTxt = (TextView)findViewById(R.id.Price);

ImageView imageView =(ImageView)findViewById(R.id.imageView2);
}
}
}

这是我的代码,我想使用 ArrayAdapter 和 ViewHolder 创建自定义 ListView

我创建了 customAdapter 类,并在 GetView 方法中尝试从数组(IMAGES、NAMES、DESC、PRICE)获取数据

当我运行代码 java.lang.NullPointerException 错误时出现

  java.lang.NullPointerException: Attempt to read from field 'android.widget.ImageView com.example.ahmadal_bargouthy.project1.MainActivity$CustomAdapter$ViewHolder.imageView' on a null object reference
at com.example.ahmadal_bargouthy.project1.MainActivity$CustomAdapter.getView(MainActivity.java:98)
at android.widget.AbsListView.obtainView(AbsListView.java:2366)
at android.widget.ListView.makeAndAddView(ListView.java:2052)
at android.widget.ListView.fillDown(ListView.java:786)
at android.widget.ListView.fillFromTop(ListView.java:847)
at android.widget.ListView.layoutChildren(ListView.java:1826)
at android.widget.AbsListView.onLayout(AbsListView.java:2165)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:444)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:753)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2792)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2319)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer.doFrame(Choreographer.java:696)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

请帮帮我

最佳答案

if (r==null){
LayoutInflater layoutInflater = context.getLayoutInflater();

r=layoutInflater.inflate(R.layout.customlayout,null,true);

viewHolder.nameTxt = r.findViewById(R.id.Name);
viewHolder.descTxt = r.findViewById(R.id.Desc);
viewHolder.priceTxt = r.findViewById(R.id.Price);
vewHolder.imageView = r.findViewById(R.id.imageView2);

r.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)r.getTag();
}


class ViewHolder{
TextView nameTxt;
TextView descTxt;
TextView priceTxt;
ImageView imageView;
}

关于java - 自定义 ListView 中的空指针异常 View 持有者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53389535/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com