对话框中的微调器 - NullPointerException - Android

发布于 2024-12-25 08:46:33 字数 1777 浏览 5 评论 0原文

我想显示一个带有微调器的自定义对话框。但是当我这样做时,我在 setAdapter() 方法中得到 NullPointerException。我已经尝试了一个多星期了,但不知道如何解决这个问题。这是我的代码:

     AlertDialog alertDialog;

     LayoutInflater inflater = 
                 (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);                          
     View layout = inflater.inflate(R.layout.form,
                                    (ViewGroup) findViewById(R.id.layout_root));

     ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
           android.R.layout.simple_spinner_item, new String[] {"0","1","2"});
     Spinner spinner = (Spinner) findViewById(R.id.spinner1);

    //I get the error in the following line:

    try{ 

         spinner.setAdapter(spinnerAdapter);

    }catch(Exception exception){
         Toast.makeText(getApplicationContext(),
    "Exception: "+exception,Toast.LENGTH_SHORT).show();
      }

     AlertDialog.Builder  builder = new AlertDialog.Builder(this);
     builder.setView(layout);
     alertDialog = builder.create();
     alertDialog.setTitle("Security");
     alertDialog.show();
   }

这是 xml 文件 form.xml:

  ?xml version="1.0" encoding="utf-8"?>


  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical"
   android:id="@+id/layout_root" >


   <Spinner
    android:id="@+id/spinner1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

  </LinearLayout> 

请帮助我。我已点击链接:对话框中的微调器 - NullPointerException 其中讨论了同样的问题,但我仍然无法做到。

I want to show a custom dialog with a spinner in it. But when i do I get NullPointerException at the setAdapter() method.I have been trying for over a week now and couldnt figure out how to get this right. here's my code:

     AlertDialog alertDialog;

     LayoutInflater inflater = 
                 (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);                          
     View layout = inflater.inflate(R.layout.form,
                                    (ViewGroup) findViewById(R.id.layout_root));

     ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
           android.R.layout.simple_spinner_item, new String[] {"0","1","2"});
     Spinner spinner = (Spinner) findViewById(R.id.spinner1);

    //I get the error in the following line:

    try{ 

         spinner.setAdapter(spinnerAdapter);

    }catch(Exception exception){
         Toast.makeText(getApplicationContext(),
    "Exception: "+exception,Toast.LENGTH_SHORT).show();
      }

     AlertDialog.Builder  builder = new AlertDialog.Builder(this);
     builder.setView(layout);
     alertDialog = builder.create();
     alertDialog.setTitle("Security");
     alertDialog.show();
   }

Here's the xml file form.xml:

  ?xml version="1.0" encoding="utf-8"?>


  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical"
   android:id="@+id/layout_root" >


   <Spinner
    android:id="@+id/spinner1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

  </LinearLayout> 

Please help me out. I've followed the link : Spinner in dialog - NullPointerException
which discusses the same problem but i still couldnt do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩套路吗 2025-01-01 08:46:34

试试这个:

Spinner spinner = (Spinner)layout.findViewById(R.id.spinner1);

Try this:

Spinner spinner = (Spinner)layout.findViewById(R.id.spinner1);

寄离 2025-01-01 08:46:34

Spinner spinner = (Spinner) findViewById(R.id.spinner1); 您不能在此处执行此操作。您需要使用 layout.findviewById(...).. (我希望您的layout_root 有微调器)。

Spinner spinner = (Spinner) findViewById(R.id.spinner1); you can't do this here. You need to use layout.findviewById(...).. (I hope your layout_root has spinner).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文