安卓:在旋转器上的问题getSelectedItem(Android: problems with

2019-10-22 21:17发布

我有一个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的调试器告诉我一个java.lang.NullPointerException该行中:

。最终字符串taglia = spinnerTaglia.getSelectedItem()的toString();

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

Answer 1:

getSelectedItem()如果没有什么在你的微调,并要求选择返回null toString()是使你的应用程序崩溃。 摆脱

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

并在您的onClick做:

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


Answer 2:

招行

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

你里面OnClickListener

目前,你想一直选择任何东西之前读所选择的项目。 你也应该确保getSelectedItem()没有返回null,因为,除非你启用/禁用btnCompilaOrdine按钮(一个项目被选中时),用户可以按下按钮,而不在微调中选择一个项目。



Answer 3:

也许你应该OnItemSelectedListener按钮inseatead。 Android的微调



Answer 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/



Answer 5:

您正在微调的实际呈现前获得选择的项目。 这取决于设备到设备,它是如何快速地渲染画面。 而不是让所选项目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[]{"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();
        } 

        } 
        }); 
    } 


Answer 6:

mSpinner.setSelected(true);

如果实施此与您的spinner ,它不会给



文章来源: Android: problems with getSelectedItem on a spinner