我在從Spinner
獲取信息並將其傳遞給其他活動時出現問題。如何通過意圖從微調器發送字符串
該意圖發送一個包含來自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);
}}
它不符合你的期望?你知道'EditText'的值是否成功地將它放入包中?你如何在接收活動中提取它? – Blumer 2013-02-18 17:37:53
如果我像這樣運行,它會在第二個活動嘗試啓動時崩潰。如果我將getText更改爲getContext,它將攜帶活動名稱並顯示在第二個活動(com.example.app.activity)上。是的,EditText的值按照它應該的那樣工作。我正在使用下面的代碼解壓縮它。不過,我認爲這部分工作正常。 – user2059311 2013-02-18 17:43:24
'Bundle bundle = getIntent()。getExtras(); \t \t String messagecolor = bundle.getString(MainActivity.EXTRA_MESSAGE_COLOR); \t \t String message = bundle.getString(MainActivity.EXTRA_MESSAGE); ' – user2059311 2013-02-18 17:43:47