【问题标题】:Android: problems with getSelectedItem on a spinnerAndroid:微调器上的 getSelectedItem 问题
【发布时间】:2015-04-30 07:49:16
【问题描述】:

我有一个Spinner,并将所选项目放在邮件正文中。 这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_modulo);

    Spinner spinnerTaglia = (Spinner) findViewById(R.id.spinnerTaglia);

// Create an ArrayAdapter using the string array and a default spinner layout ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,     R.array.Taglie, android.R.layout.simple_spinner_item);

// Specify the layout to use when the list of choices appears adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerTaglia.setPrompt("Seleziona la taglia!");

// Apply the adapter to the spinner
    spinnerTaglia.setAdapter(new NothingSelectedSpinnerAdapter(
            adapter,
            R.layout.contact_spinner_row_nothing_selected,
            // R.layout.contact_spinner_nothing_selected_dropdown, // Optional
            this));

    final String taglia = spinnerTaglia.getSelectedItem().toString();


    Button btnCompilaOrdine = (Button) findViewById(R.id.btnCompilaOrdine);
    btnCompilaOrdine.setOnClickListener(new View.OnClickListener(){

        public void onClick(View arg0) {

    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("message/rfc822");
    i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"MAIL@gmail.com"});
    i.putExtra(Intent.EXTRA_SUBJECT, "MAIL OBJECT");
    i.putExtra(Intent.EXTRA_TEXT   , "Taglia: "+taglia);
    try {
        startActivity(Intent.createChooser(i, "Send mail..."));
    } catch (android.content.ActivityNotFoundException ex) {
        Toast.makeText(Modulo.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
    }

    }
    });
}

应用程序在模拟器中正确启动,调试器什么也没显示(我正在使用 Android Studio),但是当我单击将我带入此活动的按钮时,应用程序崩溃,Android Studio 的调试器显示@987654323行中的@:

final String taglia = spinnerTaglia.getSelectedItem().toString();

我该如何解决这个问题?

【问题讨论】:

  • 似乎没有选择任何项目,因此 getSelectedItem() 返回 null。你不应该添加一个 onSelection 回调或类似的东西吗?

标签: java android nullpointerexception spinner getselection


【解决方案1】:

getSelectedItem() 如果您的微调器上没有选择任何内容,则返回 null 并且调用 toString() 会使您的应用程序崩溃。摆脱

final String taglia = spinnerTaglia.getSelectedItem().toString();

在你的 onClick 中做:

if (spinnerTaglia.getSelectedItem() == null) {
      return;
}
String taglia = spinnerTaglia.getSelectedItem().toString();
// the other code

【讨论】:

  • 好的,在单击按钮时实现此实现,使微调器正确打开的模块,但现在当我单击按钮 btnCompilaOrdine: java.lang.IllegalStateException 后,它显示微调器选择后的另一个错误: 在视图类 android.support.v7.internal.widget.TintButton 的 id 为“btnCompilaOrdine”的活动类 com.example.valeria.flexibilaapp.Modulo 中找不到 onClick 处理程序的方法 compilaOrdine(View)
  • 你可以在你的ActivityModulo中添加一个public void btnCompilaOrdine(View view) {}方法,或者在你的ActivityModulo中打开你正在使用的布局,寻找onClick属性并去掉它
  • 我不记得是什么原因了,但我把 android:onClick="compilaOrdine" 放在了按钮的 XML 属性中。我已经取消了它,但现在点击按钮没有任何反应!
  • 没有真正的原因。这是为该视图设置 onClickListener 的另一种方法。您可以通过 xml 或以编程方式调用 setOnClickListener
  • 所以,java代码: Button btnCompilaOrdine = (Button) findViewById(R.id.btnCompilaOrdine); btnCompilaOrdine.setOnClickListener(new View.OnClickListener(){ public void onClick(View arg0) { } 可以替换为 XML 属性 android:onclick="compilaOrdine ,对吧?那么,如果我删除 XML 属性,它必须有效吗?
【解决方案2】:

移动线

final String taglia = spinnerTaglia.getSelectedItem().toString();

到你的OnClickListener里面

目前,您正在尝试在选择任何内容之前读取所选项目。您还应该确保 getSelectedItem() 不返回 null,因为除非您启用/禁用 btnCompilaOrdine 按钮(选择项目时),否则用户可以在不选择微调器中的项目的情况下按下按钮。

【讨论】:

    【解决方案3】:

    也许您应该将 OnItemSelectedListener 替换为按钮。 Android Spinner

    【讨论】:

      【解决方案4】:

      似乎返回的项目带有NULL 值尝试Invoking the method from a null object

      NullPointerExceptionRuntimeException,因此,Javac 编译器不会强制您使用 try-catch 块来适当地处理它。

      希望这将帮助您解决问题。

      如需进一步参考,请访问以下链接:- http://examples.javacodegeeks.com/java-basics/exceptions/java-lang-nullpointerexception-how-to-handle-null-pointer-exception/

      【讨论】:

        【解决方案5】:

        您在实际渲染微调器之前获取所选项目。渲染屏幕的速度取决于设备到设备。 与其在onCreate() 方法中获取getSelectionItem() 中的选定项目,不如尝试在ButtononClickListener() 中执行此操作。

            @Override 
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_modulo);
        
                Spinner spinnerTaglia = (Spinner) findViewById(R.id.spinnerTaglia);
        
            // Create an ArrayAdapter using the string array and a default spinner layout ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,     R.array.Taglie, android.R.layout.simple_spinner_item); 
        
            // Specify the layout to use when the list of choices appears adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
                spinnerTaglia.setPrompt("Seleziona la taglia!");
        
            // Apply the adapter to the spinner 
                spinnerTaglia.setAdapter(new NothingSelectedSpinnerAdapter(
                        adapter, 
                        R.layout.contact_spinner_row_nothing_selected,
                        // R.layout.contact_spinner_nothing_selected_dropdown, // Optional 
                        this));
        
        
        
        
                Button btnCompilaOrdine = (Button) findViewById(R.id.btnCompilaOrdine);
                btnCompilaOrdine.setOnClickListener(new View.OnClickListener(){
        
                    public void onClick(View arg0) {
        
                //Get the Selected item from the spinner
                final String taglia = spinnerTaglia.getSelectedItem().toString();
                Intent i = new Intent(Intent.ACTION_SEND);
                i.setType("message/rfc822");
                i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"MAIL@gmail.com"});
                i.putExtra(Intent.EXTRA_SUBJECT, "MAIL OBJECT");
                i.putExtra(Intent.EXTRA_TEXT   , "Taglia: "+taglia);
                try { 
                    startActivity(Intent.createChooser(i, "Send mail..."));
                } catch (android.content.ActivityNotFoundException ex) {
                    Toast.makeText(Modulo.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
                } 
        
                } 
                }); 
            } 
        

        【讨论】:

          【解决方案6】:
          mSpinner.setSelected(true);
          

          如果您使用 spinner 实现此功能,它不会给出 null

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-05-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-06-20
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多