-1

我在从 a 获取信息Spinner并将其通过意图传递给另一个活动时遇到问题。

意图发送一个包,其中包含来自 an 的字符串EditText以及来自 的项目Spinner

我认为问题是由于getText()下面的行。但是我不确定如何解决这个问题。

String messagecolor = editTextcolor.getText().toString();

任何人都可以提供有关问题所在的见解吗?

 package com.example.myfirstapp;

    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemSelectedListener;
    import android.widget.ArrayAdapter;
    import android.widget.EditText;
    import android.widget.Spinner;

    public class MainActivity extends Activity {
        public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
        public final static String EXTRA_MESSAGE_COLOR = "com.example.myfirstapp.MESSAGE2";

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

            Spinner spinner = (Spinner) findViewById(R.id.spinner);
            // Create an ArrayAdapter using the string array and a default spinner layout
            ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                    R.array.color_array, 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);
            // Apply the adapter to the spinner
            spinner.setAdapter(adapter);


        class SpinnerActivity extends Activity implements OnItemSelectedListener {
            public void onItemSelected(AdapterView<?> parent, View view, 
                    int pos, long id) {
                // An item was selected. You can retrieve the selected item using
                // parent.getItemAtPosition(pos)
            }

            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback

            Spinner spinner = (Spinner) findViewById(R.id.spinner);
            spinner.setOnItemSelectedListener(this);
            }
        }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }  

    /** Called when the user clicks the Send button */
    public void sendMessage (View view)   {
        Intent i = new Intent(this, DisplayMessageActivity.class);
        EditText editText = (EditText) findViewById(R.id.edit_message);
        Spinner editTextcolor = (Spinner) findViewById(R.id.spinner);
        String message = editText.getText().toString();
        String messagecolor = editTextcolor.getText().toString();
        Bundle extras = new Bundle();
        extras.putString(EXTRA_MESSAGE, message);
        extras.putString(EXTRA_MESSAGE_COLOR, messagecolor );
        i.putExtras(extras);
        startActivity(i);

    }}  
4

1 回答 1

1

您可能面临的问题是您没有获得所选项目的字符串。您可以尝试使用getSelectedItem()

String messagecolor = editTextcolor.getSelectedItem().toString();

这个方法可以返回null,所以你也应该检查一下。

于 2013-02-18T17:42:36.910 回答