这个问题在这里已经有答案了:
什么是nullpointerexception,如何修复它(12个答案)
findviewbyid返回null(29个答案)
两年前关门了。
我正在申请一个 ArrayList
由两个 ListViews
在纵向和横向上。为此,我使用了一个自定义适配器来扩展我的列表视图。但我一直收到一封信 NullPointer Exception
每当我为我的 landscape ListView
. 有什么建议吗?引发异常的行:
listLand.setAdapter(customAdapter);
主要活动
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;
}
}
}
1条答案
按热度按时间piwo6bdm1#
猜测一下:你有两种不同的布局——一种是纵向的,另一种是横向的。只有一个会通过
findViewById
,取决于设备方向。请记住,活动是重新创建的(并且onCreate
调用)当您旋转设备时。你应该检查一下listPort
以及listLand
为了null
设置适配器之前。你不能同时使用两者。像这样:
其中一个应该是非空的,前提是其他一切都正常工作。
我猜,如果你有
values/activity_main.xml
还有一个values-land/activity_main.xml
,您可以通过在两个位置使用相同的列表id使自己的生活更轻松。例如。,