2 markdowneide markdowneide 于 2013.09.10 16:45 提问

inflated View上的NullpointerException

在 AlertDialog 中有一个 inflated layout 。当我提及到它其中的一个 view 时,获取一个 NullpointerException。

dialog_with_spinner.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:padding="20dp">

        <TextView
            android:id="@+id/info"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:text="@string/info_title" />

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:entries="@array/entries" 
            android:saveEnabled="true"/>

    </LinearLayout>

    <CheckBox
        android:id="@+id/someId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/someString"/>

</LinearLayout>

Code:

AlertDialog.Builder builder = new AlertDialog.Builder(boxThemeContextWrapper);
LayoutInflater inflater = getLayoutInflater();

builder.setView(inflater.inflate(R.layout.dialog_with_spinner, null))
       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
           @Override
           public void onClick(DialogInterface dialog, int id) {

               final Spinner sp = (Spinner) findViewById(R.id.spinner);

               int p = sp.getSelectedItemPosition();  // this gets the **NullPointerException**

               String[] entryValues = getResources().getStringArray(R.array.entry_values);
               final String entry = entryValues[p];

               useMyEntryMethod(entry);
           }
       })
       .setNegativeButton(R.string.dialogs_negative, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               //
           }
       });      

builder.show();

如何处理这个问题呢?

1个回答

lsh009
lsh009   2013.09.10 17:13
已采纳

(Spinner) findViewById(R.id.spinner);这里其实返回的是空的,你应该用
inflater.inflate(R.layout.dialog_with_spinner, null)这个返回的view,再view.findViewById,

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout's ro
今天在用inflater时候,我看了看代码并没有错。可是一点击Oncreate的按钮,就崩了,再看一直null那显示黄色警告,鼠标碰上去发现这句话:   Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout's root element)   我的代码:
关于LayoutInflater的总结
首先先看一下Android Studio Lint的警告: Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout's root element) less... (Ctrl+F1) When inflating a layout, avoid passing in null as the parent view, since otherwise any lay
Android笔记之使用LayoutInflater创建View
常见的使用方法 1. View view = LayoutInflater.from(mContext).inflate(resource, root); /** * Inflate a new view hierarchy from the specified xml resource. Throws * {@link InflateException} if ther
如何解决NullPointerException in android.view.GLES20RecordingCanvas
一、如何解决NullPointerException in android.view.GLES20RecordingCanvas 在发布版本的最后一轮过设备时,在某老旧设备上出现了空指针异常 java.lang.NullPointerExceptionat android.view.GLES20RecordingCanvas.drawPatch(GLES20RecordingCanvas.
butterknife官方文档翻译
butterknife使用原文链接:http://jakewharton.github.io/butterknife/github地址:https://github.com/JakeWharton/butterknife介绍ButterKnife使用@BindView和view ID 注解成员变量,自动地绑定你布局中对应的viewclass ExampleActivity extends Activ
findViewById中NullPointerException的错误
findViewById中NullPointerException的错误
ViewPager findViewById错误NullPointerException
advPager = (ViewPager) getView().findViewById(R.id.adv_pager); 08-07 12:51:16.041: E/AndroidRuntime(29103): Caused by: java.lang.NullPointerException Fragment工程中,initViewPager在onActivityC
ButterKnife 8.0.1 @BindView 失败,nullpointerexception
ButterKnife 8.0.1  @BindView 失败,空指针异常,跟踪进去,发现是XXXFragment的 XXXFragment$$ViewBinder 的类没有生成。 经过google。发现了解决方案。记录一下。 ButterKnife 8.0.1 not working Per the readme, you need to incl
ButterKnife BindView 失败,报错NullPointerException
ButterKnife BindView 失败,报错NullPointerException,得到的View是null原因是配置有误。第一步apply plugin: 'com.jakewharton.butterknife' 第二步compile 'com.jakewharton:butterknife:8.5.1' annotationProcessor 'com.jakewharton:but
inflate (int resource, ViewGroup root)关于root 这个参数的最佳解释
文档: public View inflate (int resource, ViewGroup root) .... returns The root View of the inflated hierarchy. If root was supplied, this is the root View; otherwise it is the root of the inflated X