0

整理完我的代码后,当我点击提交按钮时,我的第二个屏幕上一直显示为空,无法发现我的错误,任何帮助将不胜感激。

头等舱

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>
4

1 回答 1

0

你来做这件事:

nextScreen.putExtra("USERNAME", iwi1);

nextScreen然后使用Intent启动第二个 Activity 。您永远不会使用intent具有"spinnerText"密钥的 Intent。

然后,当你在第二个活动中获得额外的东西时,你正在做

String text = intent.getStringExtra("spinnerText");

其中引用了一个不存在的意图键 ( nextScreen),因此,文本将是null。在屏幕上您最有可能看到First Iwi null

考虑将其更改为:

String text = intent.getStringExtra("USERNAME"); 

反而。

于 2013-01-21T00:32:38.983 回答