意图null错误

时间:2013-01-21 00:26:42

标签: android android-intent android-spinner

在整理出我的代码之后,当我点击提交按钮时发现我的错误,我继续在第二个屏幕上得到一个null,任何帮助将不胜感激。

头等舱

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>

1 个答案:

答案 0 :(得分:0)

你这样做:

nextScreen.putExtra("USERNAME", iwi1);

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

然后,当你在第二个Activity中获得额外的时,你正在做

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

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

考虑将其更改为:

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

代替。