无法加载由Spinner传递的意图额外数据

时间:2014-01-19 06:38:34

标签: android android-layout android-intent android-spinner

我尝试将一个活动数据传递给另一个活动。这些数据来自Spinners。当我尝试运行该应用程序时,它显示错误。有谁可以帮助我?

这是xml

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_alignLeft="@+id/planets_spinner"
        android:layout_below="@+id/textView1"
        android:entries="@array/price_list" />



    <Spinner
        android:id="@+id/planets_spinner"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_alignLeft="@+id/textView4"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="19dp"
        android:entries="@array/Food" />

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_alignLeft="@+id/planets_spinner"
        android:layout_below="@+id/textView3"
        android:layout_marginTop="16dp"
        android:entries="@array/dress" />
 <Spinner
        android:id="@+id/spinner3"
        android:layout_width="200dp"
        android:layout_height="40dp"
        android:layout_alignLeft="@+id/textView4"
        android:layout_below="@+id/textView4"
        android:layout_marginTop="30dp"
        android:entries="@array/time" />

这是第一项活动

Spinner dress,food,price,time;
    String txt_dress,txt_price,txt_food,txt_time,restName;
    Button btnSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.advance_searech);

        dress = (Spinner) findViewById(R.id.spinner1);
        price= (Spinner) findViewById(R.id.planets_spinner);
        food= (Spinner) findViewById(R.id.spinner2);
        time= (Spinner) findViewById(R.id.spinner3);

        btnSearch = (Button) findViewById(R.id.button1);

        btnSearch.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                txt_dress = dress.getSelectedItem().toString();
                txt_food = food.getSelectedItem().toString();
                txt_price = price.getSelectedItem().toString();
                txt_time = time.getSelectedItem().toString();
                Log.d("Dress", txt_dress);

                Intent intent = new Intent(AdvanceSearch.this,AdvanceSearchResult.class);
                intent.putExtra("txt_price", txt_price);
                intent.putExtra("txt_dress", txt_dress);
                intent.putExtra("txt_time", txt_time);
                intent.putExtra("txt_food", txt_food);
                startActivity(intent);
            }
        }); 
    }

这是第二项活动

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.advance_result);

    Intent i = getIntent();
    String price = i.getStringExtra("txt_price");

    Toast.makeText(getApplicationContext(), price, Toast.LENGTH_LONG).show();

}

这是错误

01-19 12:07:02.571: E/AndroidRuntime(10020): FATAL EXCEPTION: main
01-19 12:07:02.571: E/AndroidRuntime(10020): java.lang.NullPointerException
01-19 12:07:02.571: E/AndroidRuntime(10020):    at com.example.rp.AdvanceSearch$1.onClick(AdvanceSearch.java:46)
01-19 12:07:02.571: E/AndroidRuntime(10020):    at android.view.View.performClick(View.java:4191)
01-19 12:07:02.571: E/AndroidRuntime(10020):    at android.view.View$PerformClick.run(View.java:17229)
01-19 12:07:02.571: E/AndroidRuntime(10020):    at android.os.Handler.handleCallback(Handler.java:615)
01-19 12:07:02.571: E/AndroidRuntime(10020):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-19 12:07:02.571: E/AndroidRuntime(10020):    at android.os.Looper.loop(Looper.java:137)
01-19 12:07:02.571: E/AndroidRuntime(10020):    at android.app.ActivityThread.main(ActivityThread.java:4960)
01-19 12:07:02.571: E/AndroidRuntime(10020):    at java.lang.reflect.Method.invokeNative(Native Method)
01-19 12:07:02.571: E/AndroidRuntime(10020):    at java.lang.reflect.Method.invoke(Method.java:511)
01-19 12:07:02.571: E/AndroidRuntime(10020):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
01-19 12:07:02.571: E/AndroidRuntime(10020):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
01-19 12:07:02.571: E/AndroidRuntime(10020):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

此处pricetime null 因为您没有初始化,而且您在onCLick

进行
price.getSelectedItem().toString();
time.getSelectedItem().toString();

所以改变

dress = (Spinner) findViewById(R.id.spinner1);
food = (Spinner) findViewById(R.id.planets_spinner);
dress = (Spinner) findViewById(R.id.spinner2);
food = (Spinner) findViewById(R.id.spinner3);

dress = (Spinner) findViewById(R.id.spinner1);
food = (Spinner) findViewById(R.id.planets_spinner);
price = (Spinner) findViewById(R.id.spinner2);
time = (Spinner) findViewById(R.id.spinner3);