整理完我的代码后,当我点击提交按钮时,我的第二个屏幕上一直显示为空,无法发现我的错误,任何帮助将不胜感激。
头等舱
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;
public class IwiSelect extends Activity {
Spinner spinner1, spinner2, spinner3, spinner4, spinner5;
Button btnSubmit;
String iwi1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_iwiselect);
addListenerOnButton();
}
@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_iwi_select, menu);
return true;
}
// get the selected drop down list value
public void addListenerOnButton() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner3 = (Spinner) findViewById(R.id.spinner3);
spinner4 = (Spinner) findViewById(R.id.spinner4);
spinner5 = (Spinner) findViewById(R.id.spinner5);
btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
String text = spinner1.getSelectedItem().toString();
//Starting a new Intent
Intent intent = new Intent();
intent.putExtra("spinnerText", text);
Intent nextScreen = new Intent(IwiSelect.this, SecondScreenActivity.class);
//Sending data to another Activity
nextScreen.putExtra("USERNAME", iwi1);
//nextScreen.putExtra(spinner2.getContext().toString(), false);
// starting new activity
startActivity(nextScreen);
}
});
}
}
用户从微调器中选择的第一类(总共有 5 个微调器)
二等
public class SecondScreenActivity extends Activity {
TextView FirstIwi;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selected_iwi);
FirstIwi = (TextView) findViewById(R.id.textView1);
Intent intent = getIntent();
String text = intent.getStringExtra("spinnerText");
//Spinner spinnername2 = (Spinner) findViewById(R.id.spinner2);
FirstIwi.setText("First Iwi " + text);
}
}
此活动用于从微调器中选择一个选项
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".IwiSelect" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/IwiList"
android:prompt="@string/Iwi_prompt" />
<Spinner
android:id="@+id/spinner4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/spinner5"
android:layout_alignParentLeft="true"
android:layout_marginBottom="70dp"
android:entries="@array/IwiList"
android:prompt="@string/Iwi_prompt" />
<Spinner
android:id="@+id/spinner3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/spinner5"
android:layout_marginTop="24dp"
android:entries="@array/IwiList"
android:prompt="@string/Iwi_prompt" />
<Spinner
android:id="@+id/spinner5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/spinner1"
android:layout_marginTop="90dp"
android:entries="@array/IwiList"
android:prompt="@string/Iwi_prompt" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/spinner3"
android:layout_marginTop="14dp"
android:entries="@array/IwiList"
android:prompt="@string/Iwi_prompt" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="onClick"
android:text="Submit" />
</RelativeLayout>
此活动用于显示选定的选项
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".IwiSelect" >
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="TEXT"/>
</RelativeLayout>