您好我知道之前已发布但我似乎无法找到错误。我创建了一个List<Objects>
,我想用它来填充一个微调器。我的代码
public class RegisterActivity extends AppCompatActivity {
private Toolbar tb;
private Spinner spinner;
private List<Swmata> lista;
private BaseAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
spinner = (Spinner)findViewById(R.id.spinner3);
tb = (Toolbar)findViewById(R.id.toolBar);
lista = new ArrayList<Swmata>();
tb.setTitle("Λελέμετρο");
setSupportActionBar(tb);
populateSpinner();
adapter = new MyAdapter(RegisterActivity.this, lista);
spinner.setAdapter(adapter);
}
private void populateSpinner() {
lista.add(new Swmata(R.drawable.peziko, "Πεζικό(ΠΖ)"));
lista.add(new Swmata(R.drawable.eidikesdinameis, "Ειδικές Δυνάμεις(ΕΔ)"));
lista.add(new Swmata(R.drawable.tethwrakismena, "Τεθωρακισμένα(ΤΘ)"));
lista.add(new Swmata(R.drawable.piroboliko, "Πυροβολικό(ΠΒ)"));
lista.add(new Swmata(R.drawable.mixaniko, "Μηχανικό(ΜΧ)"));
lista.add(new Swmata(R.drawable.aeroporia, "Αεροπορία Στρατού(ΑΣ)"));
lista.add(new Swmata(R.drawable.texniko, "Τεχνικό(ΤΧ)"));
lista.add(new Swmata(R.drawable.efodiasmou, "Εφοδιασμού Μεταφορών(ΕΜ)"));
lista.add(new Swmata(R.drawable.ylikou, "Υλικού Πολέμου(ΥΠ)"));
lista.add(new Swmata(R.drawable.ygeionomiko, "Υγειονομικό(ΥΓ)"));
lista.add(new Swmata(R.drawable.diabibaseis, "Διαβιβάσεων(ΔΒ)"));
lista.add(new Swmata(R.drawable.stratonomia, "Στρατονομία(ΣΝ)"));
}
适配器:
public class MyAdapter extends BaseAdapter {
private List<Swmata> objects;
private Context context;
public MyAdapter(Context context, List objects) {
this.context = context;
this.objects = objects;
}
@Override
public int getCount() {
return objects.size();
}
@Override
public Object getItem(int position) {
return objects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SwmataHolder holder;
if (convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.spinner_item, parent, false);
holder = new SwmataHolder();
holder.imageView = (ImageView)convertView.findViewById(R.id.imageSpinner5);
holder.textView = (TextView)convertView.findViewById(R.id.textSpinner5);
convertView.setTag(holder);
}else {
holder = (SwmataHolder)convertView.getTag();
}
Swmata swma = (Swmata) getItem(position);
holder.imageView.setImageResource(swma.getImage());
holder.textView.setText(swma.getName());
return convertView;
}
static class SwmataHolder{
ImageView imageView;
TextView textView;
}
}
错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.project.matrix.lelemetroga.MyAdapter.getView(MyAdapter.java:46)
第46行:holder.imageView = (ImageView)convertView.findViewById(R.id.imageSpinner5);
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:padding="15dp"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/aeroporia"
android:layout_weight="1"
android:id="@+id/imageSpinner5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:layout_weight="1"
android:id="@+id/textSpinner5" />
</LinearLayout>
现在我不知道我做错了什么,但因为我在其他项目中使用相同的方法,它实际上看起来很有趣,我找不到我的错误。如果有人能指出它我宁愿赞赏。在此先感谢!!!
答案 0 :(得分:2)
在此代码段中,您测试convertView
是否为空,但是您继续并取消引用它:
if (convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.spinner_item, parent, false);
holder = new SwmataHolder();
holder.imageView = (ImageView)convertView.findViewById(R.id.imageSpinner5);
holder.textView = (TextView)convertView.findViewById(R.id.textSpinner5);
convertView.setTag(holder);
}else {
我认为您打算为convertView
分配一个值,如下所示:
if (convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.spinner_item, parent, false);
holder = new SwmataHolder();
holder.imageView = (ImageView)convertView.findViewById(R.id.imageSpinner5);
holder.textView = (TextView)convertView.findViewById(R.id.textSpinner5);
convertView.setTag(holder);
}else {
答案 1 :(得分:1)
您忘记将膨胀值分配给convertView变量。
此:
inflater.inflate(R.layout.spinner_item, parent, false);
应该是这样的:
convertView = inflater.inflate(R.layout.spinner_item, parent, false);
答案 2 :(得分:1)
你需要
convertView = inflater.inflate(R.layout.spinner_item, parent, false);
在if
声明中。
您在NPE
上获得convertView.findViewById()
,因为您从未初始化convertView
,因此它是第一次null
。在膨胀View
时需要给它一个值。