我正在编写一个应用程序,其中ArrayList
由两个ListViews
代表纵向和横向。为此,我一直在使用一个自定义适配器来为其listViews充气。但是,每当实例化NullPointer Exception
的自定义适配器时,我都会不断收到landscape ListView
。有什么建议么?
抛出异常的行:
listLand.setAdapter(customAdapter);
MainActivity
public class MainActivity extends AppCompatActivity {
ListView listPort;
ListView listLand;
ArrayList<Flick> flicks;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listLand = findViewById(R.id.listLand);
listPort = findViewById(R.id.listPort);
flicks = new ArrayList<>();
flicks.add(new Flick("Wizard of Oz", "Metro-Goldwyn-Mayer", "VARIOUS","1939", "", 98));
flicks.add(new Flick("Star Wars", "LucasFilm", "George Lucas", "1977","",93));
flicks.add(new Flick("Psycho","Paramount", "Alfred Hitchcock", "1960", "", 97));
flicks.add(new Flick("King Kong", "RKO Pictures", "Merian C. Cooper & Ernest B. Schoedsack", "1933", "", 98));
flicks.add(new Flick("2001: A Space Odyssey", "Metro-Goldwyn-Mayer", "Stanley Kubrick", "1968", "", 93));
flicks.add(new Flick("Citizen Kane", "RKO Pictures", "Orson Welles", "1941", "", 100));
flicks.add(new Flick("Snow White and the Seven Dwarfs", "Walt Disney Pictures", "VARIOUS", "1937", "",98));
flicks.add(new Flick("Casablanca", "Warner Bros.", "Michael Curtiz", "1942", "", 97));
flicks.add(new Flick("The Godfather", "Paramount", "Francis Ford Coppola", "1972", "", 98));
flicks.add(new Flick("Jaws", "Universal", "Steven Spielberg", "1975", "", 97));
CustomAdapter customAdapter = new CustomAdapter(this, R.layout.custom_layout, flicks);
listPort.setAdapter(customAdapter);
listLand.setAdapter(customAdapter);
}
public class CustomAdapter extends ArrayAdapter<Flick>
{
Context context;
int resource;
List<Flick> list;
public CustomAdapter(@NonNull Context context, int resource, @NonNull List<Flick> objects) {
super(context, resource, objects);
this.context=context;
this.resource=resource;
list=objects;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater layoutInflater=(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View adapterView=layoutInflater.inflate(resource,null);
TextView title = adapterView.findViewById(R.id.title);
ImageView imageView=adapterView.findViewById(R.id.imageView);
Button up=adapterView.findViewById(R.id.upButton);
Button down=adapterView.findViewById(R.id.downButton);
title.setText(list.get(position).getTitle());
switch(list.get(position).getStudio())
{
case "Warner Bros.":
imageView.setImageResource(R.drawable.warnerbros);
break;
case "Paramount":
imageView.setImageResource(R.drawable.warnerbros);
break;
case "Universal":
imageView.setImageResource(R.drawable.warnerbros);
break;
case "LucasFilm":
imageView.setImageResource(R.drawable.warnerbros);
break;
case "RKO Pictures":
imageView.setImageResource(R.drawable.warnerbros);
break;
case "Walt Disney Pictures":
imageView.setImageResource(R.drawable.warnerbros);
break;
case "Metro-Goldwyn-Mayer":
imageView.setImageResource(R.drawable.warnerbros);
break;
}
return adapterView;
}
}
}
答案 0 :(得分:0)
一个猜测:您有两种不同的布局-一种用于纵向,另一种用于横向。根据设备的方向,将仅通过findViewById
加载其中之一。请记住,旋转设备会重新创建活动(并调用onCreate
)。设置适配器之前,您应该只检查listPort
的{{1}}和listLand
。您不会同时使用两者。
类似这样的东西:
null
其中一个应该为非null,前提是其他所有功能均应正常工作。
如果我猜您有一个if (listPort != null) {
listPort.setAdapter(customAdapter); // Portrait mode
} else {
listLand.setAdapter(customAdapter); // Landscape mode
}
和一个values/activity_main.xml
,则可以通过在两个地方使用相同的列表ID来使自己的生活更轻松。例如,
values-land/activity_main.xml