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创建自定义列表视图的代码
我创建了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)
请帮助我
答案 0 :(得分:1)
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;
}
答案 1 :(得分:0)
如果您需要初始化类的字段,则应使用“ static”修饰符,这可能会解决问题