【发布时间】:2016-12-29 04:55:40
【问题描述】:
原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'
我有一个自定义 dialog,其中包含一个 ViewPager,其中包含一个 listview,但是在尝试设置我的 ArrayAdapter 时出现 NullPointerException:
dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.voicedialog);
dialog.setCanceledOnTouchOutside(false);
dialog.getWindow().setLayout((int)(getResources().getDisplayMetrics().widthPixels*0.90), (int)(getResources().getDisplayMetrics().heightPixels*0.90));
adapter = new MyPageAdapter(this);
pager = (ViewPager) dialog.findViewById(R.id.viewpager);
pager.setOffscreenPageLimit(3);
pager.setAdapter(adapter);
dialog.show();
if(pager.getCurrentItem()==0) { //If on first page of viewpager in dialog (this layout contains the listview)
arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, contactArrayList);
listview = (ListView) dialog.findViewById(R.id.listView);
listview.setAdapter(arrayAdapter);
listview.setEmptyView(dialog.findViewById(R.id.emptyElement));
arrayAdapter.notifyDataSetChanged();
}
同样,列表视图位于我的对话框中我的 viewpager 的布局之一中,ID 为listView
我的问题是,为什么列表视图显示为空,即使我在上面的代码中明确引用了它,我该如何解决这个问题?
感谢您宝贵的时间。
编辑:
语音对话:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/round"
tools:context="...">
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
ViewPager 中有三个 XML 布局,但带有 listview 的布局是第一页,这就是我将代码放在 if(pager.getCurrentItem()==0) 中的原因。
这里是ViewPagerAdapter:
public class MyPageAdapter extends PagerAdapter {
private Context mContext;
LayoutInflater inflater;
public MyPageAdapter(Context context) {
mContext = context;
inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public Object instantiateItem(ViewGroup collection, int position) {
View view = null;
if(position==0) {
view = inflater.inflate(R.layout.voice1, null);
collection.addView(view);
}if(position==1) {
view = inflater.inflate(R.layout.voice2, null);
collection.addView(view);
}if(position==2) {
view = inflater.inflate(R.layout.voice3, null);
collection.addView(view);
}
return view;
}
@Override
public int getCount() {
return 3;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
}
【问题讨论】:
-
您说寻呼机包含列表视图。
listview = (ListView) dialog.findViewById(R.id.listView)对话框将无法直接找到它 -
您是否在视图寻呼机中使用片段?
-
我们能看到语音对话框的布局吗?
-
@android_griezmann 那么如何引用我的
listview? -
@quicklearner 不,我使用的是常规 XML 布局。我已经用更多细节编辑了我的问题
标签: java android xml string layout