Android:微调器上的getSelectedItem问题

问题描述 投票:1回答:6

我有一个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[]{"[email protected]"});
    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的调试器显示行中的java.lang.NullPointerException

final String size = spinnerTaglia.getSelectedItem()。toString();

我怎样才能解决这个问题?

java android nullpointerexception spinner getselection
6个回答
3
投票

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

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

并在你的onClick做:

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

1
投票

移动线

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

到你的OnClickListener里面

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


0
投票

也许你应该OnItemSelectedListener一个按钮的inseatead。 Android Spinner


0
投票

似乎Item返回NULL值尝试Invoking the method from a null object

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

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

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


0
投票

您在实际渲染微调器之前获取所选项目。这取决于设备到设备的呈现速度有多快。而不是在getSelectionItem()方法中获取onCreate()中的选定项目,尝试在onClickListener()Button中进行。

    @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[]{"[email protected]"});
        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();
        } 

        } 
        }); 
    } 

0
投票
mSpinner.setSelected(true);

如果你用spinner实现它,它将不会给出null。

© www.soinside.com 2019 - 2024. All rights reserved.