【问题标题】:Nullpointerexception in dialog?对话框中的空指针异常?
【发布时间】: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


【解决方案1】:

因为ListViewViewPager里面,所以需要使用ViewPager中的当前页面View来访问ListView。为此使用 setTag/getTag 方法,如:

1.MyPageAdapterinstantiateItem方法中为每个布局添加标签:

   ....
   view.setTag("current_page_" + position);
   return view;
 }

2. 使用findViewWithTag 方法和pager.getCurrentItem() 获取当前页面视图:

View currentView = (View)pager.findViewWithTag(
       "current_page_" + pager.getCurrentItem());

3. 现在使用 currentView 访问 ListView:

    listview = (ListView) currentView.findViewById(R.id.listView);

【讨论】:

  • 执行此操作后我仍然收到错误,但略有不同
  • 让我们在聊天中继续讨论
  • 我收到Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
  • @RuchirBaronia:加入聊天
  • 我们还能继续聊天吗?
【解决方案2】:

此 xml 没有列表视图。你是这样设置的dialog.setContentView(R.layout.voicedialog); 并试图从这个对话框中获取。不包含 Listview 会自动变为 null'

编辑 1: 在 viewpager 中,您正在添加 Listview,因此您需要在 ViewPager 适配器中添加功能。

编辑 2:

您需要在 Viewpager 中添加以下代码

if(position==0) {
             view = inflater.inflate(R.layout.voice1, null);
            collection.addView(view);


arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, contactArrayList);
        listview = (ListView) view .findViewById(R.id.listView);
        listview.setAdapter(arrayAdapter);
        arrayAdapter.notifyDataSetChanged();
}

【讨论】:

  • 那么我如何获得listview
  • 好的,检查我的编辑我发布了我的浏览器。那我现在怎么引用呢?
  • 你有 R.layout.voice1,voice2,voice3... 这些包含你的列表视图对吗?
  • 不,这是我的 ViewPager 中的三个页面。只有voice1 包含列表视图
  • 检查我的编辑你没有contactArrayListhere。我希望你可以通过构造函数..
猜你喜欢
  • 1970-01-01
  • 2021-08-25
  • 2014-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-07
  • 2018-09-30
  • 1970-01-01
相关资源
最近更新 更多