嗨,大家好,我正在开发一个应用程序。我想让使用者从微调器中选择他/她的生日。我将根据spinner3更改天数。如果用户选择一个有30天的月份,则我的应用程序需要将微调框4设置为仅包含不超过30的数字。当用户选择某个微调框3时,我需要此设置。但是,当我打开应用程序时,它崩溃了。如果我注释掉这一行,它不会崩溃:
    spinner3.setOnItemSelectedListener(this); (解决了)

新问题:仅运行onItemSelected(AdapterView parent,View view,int pos,long id)

希望您能找到问题所在。谢谢

SignUp2.java:

package com.geniboys.sosyaaal;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class SignUp2 extends Activity implements
AdapterView.OnItemSelectedListener {

    Spinner spinner4 = (Spinner) findViewById(R.id.spinner4);
    Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
    Spinner spinner3 = (Spinner) findViewById(R.id.spinner3);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up2);

        ActionBar actionBar = getActionBar();
        actionBar.hide();


        List<String> list = new ArrayList<String>();

        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);

        for(int i = 1972; i <= year+4; ++i )
        {
            String foo = Integer.toString(i);
            list.add(foo);
        }

        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner2.setAdapter(dataAdapter);

        spinner3.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.sign_up2, menu);
        return true;
    }


        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
        {
            // An item was selected. You can retrieve the selected item using
            String ay = (String) parent.getItemAtPosition(pos);

            List<String> list = new ArrayList<String>();

            if( ay == "Ocak" || ay == "Mart" || ay == "Mayıs" || ay ==  "Temmuz" || ay ==  "Ekim" || ay ==  "Aralık")
            {
                for(int i = 1; i<=31; ++i )
                {
                    String foo = Integer.toString(i);
                    list.add(foo);
                }
            }
            if( ay == "Şubat" )
            {
                for(int i = 1; i<=28; ++i )
                {
                    String foo = Integer.toString(i);
                    list.add(foo);
                }
            }
            else
            {
                for(int i = 1; i<=30; ++i )
                {
                    String foo = Integer.toString(i);
                    list.add(foo);
                }
            }



        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(SignUp2.this, android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner4.setAdapter(dataAdapter);
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0)
    {

    }



    public void geri( View view )
    {
        super.onBackPressed();
    }

    public void ileri( View view)
    {

    }

}


activity_sign_up2.xml:

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/registerbackground"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".SignUpActivity" >

    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView4"
        android:layout_alignRight="@+id/editText1"
        android:onClick="ileri"
        android:text="@string/layoutIleri"
        android:textColor="#ffffff" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="28dp"
        android:layout_marginTop="70dp"
        android:text="@string/loginTanim"
        android:textColor="#ffffff"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="@string/loginKayit"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ffffff" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView4"
        android:layout_alignLeft="@+id/textView1"
        android:onClick="geri"
        android:text="@string/layoutGeri"
        android:textColor="#ffffff" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_toRightOf="@+id/textView1"
        android:ems="10"
        android:hint="@string/loginAdhint"
        android:textColor="#ffffff" />

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/editText1"
        android:layout_alignTop="@+id/textView3"
        android:layout_toRightOf="@+id/textView3"
        android:textColor="#ffffff" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView2"
        android:layout_alignLeft="@+id/textView1"
        android:layout_alignRight="@+id/textView2"
        android:textColor="#ffffff"
        android:ems="10" >

    </EditText>

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:layout_toRightOf="@+id/editText2"
        android:textColor="#ffffff"
        android:ems="10" />
    <requestFocus />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/spinner2"
        android:layout_alignTop="@+id/textView2"
        android:entries="@array/loginSehirler"
        android:prompt="@string/loginMemlekethint"
        android:textColor="#ffffff" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/TextView01"
        android:layout_below="@+id/editText3"
        android:layout_marginTop="24dp"
        android:text="@string/loginMemleket"
        android:textColor="#ffffff"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/spinner2"
        android:layout_toLeftOf="@+id/spinner2"
        android:text="@string/loginDogumTarihi"
        android:textColor="#ffffff"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView4"
        android:layout_below="@+id/spinner1"
        android:text="@string/loginMezuniyet"
        android:textColor="#ffffff"
        android:textSize="20sp" />

    <Spinner
        android:id="@+id/spinner3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/TextView01"
        android:layout_marginTop="11dp"
        android:layout_toLeftOf="@+id/editText3"
        android:entries="@array/loginAylar"
        android:prompt="@string/loginAylarhint" />

    <Spinner
        android:id="@+id/spinner5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/spinner4"
        android:layout_alignLeft="@+id/spinner2"
        android:layout_alignParentRight="true" />

    <Spinner
        android:id="@+id/spinner4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/spinner3"
        android:layout_alignTop="@+id/spinner3"
        android:layout_toLeftOf="@+id/spinner5"
        android:layout_toRightOf="@+id/spinner3" />

</RelativeLayout>


编辑:添加LogCat:

01-23 18:31:49.730: W/InputEventReceiver(28607): Attempted to finish an input event but the input event receiver has already been disposed.
01-23 18:31:49.730: W/InputEventReceiver(28607): Attempted to finish an input event but the input event receiver has already been disposed.
01-23 18:31:53.150: W/InputEventReceiver(28607): Attempted to finish an input event but the input event receiver has already been disposed.
01-23 18:31:57.930: W/InputEventReceiver(28607): Attempted to finish an input event but the input event receiver has already been disposed.
01-23 18:32:00.270: W/InputEventReceiver(28607): Attempted to finish an input event but the input event receiver has already been disposed.
01-23 18:32:10.310: W/InputEventReceiver(28607): Attempted to finish an input event but the input event receiver has already been disposed.


编辑:添加答案

我不能回答我的问题。请将此屁股添加为答案:

在此处更改if语句:

    if( ay == "Ocak" || ay == "Mart" || ay == "Mayıs" || ay ==  "Temmuz" || ay ==  "Ekim" || ay ==  "Aralık")
    {
        for(int i = 1; i<=31; ++i )
        {
            String foo = Integer.toString(i);
            list.add(foo);
        }
    }
    if( ay == "Şubat" )
    {
        for(int i = 1; i<=28; ++i )
        {
            String foo = Integer.toString(i);
            list.add(foo);
        }
    }
    else
    {
        for(int i = 1; i<=30; ++i )
        {
            String foo = Integer.toString(i);
            list.add(foo);
        }
    }




        if( ay.equals("Ocak") || ay.equals("Mart") || ay.equals("Mayıs") || ay.equals("Temmuz") || ay.equals("Ekim") || ay.equals("Aralık"))
        {
            for(int i = 1; i<=31; ++i )
            {
                String foo = Integer.toString(i);
                list.add(foo);
            }
        }
        else if( ay.equals("Şubat") )
        {
            for(int i = 1; i<=28; ++i )
            {
                String foo = Integer.toString(i);
                list.add(foo);
            }
        }
        else
        {
            for(int i = 1; i<=30; ++i )
            {
                String foo = Integer.toString(i);
                list.add(foo);
            }
        }


并在方法中声明微调框可以解决该问题。谢谢大家的帮助。

最佳答案

您不能在方法外部初始化Views。这将尝试在设置layout之前初始化它们,这意味着它们将成为null,因为它们存在于您的layout中。更改为

public class SignUp2 extends Activity implements
AdapterView.OnItemSelectedListener {

//declare here
Spinner spinner4;
Spinner spinner2;
Spinner spinner3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_up2);

    //initialize here after you have called setContentView()
    spinner4 = (Spinner) findViewById(R.id.spinner4);
    spinner2 = (Spinner) findViewById(R.id.spinner2);
    spinner3 = (Spinner) findViewById(R.id.spinner3);


脱离主题在某些时候,您将需要学习为View和其他变量指定更具体,有意义的名称。您有3个Spinners,并且知道哪个实际上会做些什么,例如,当它们表示truckSpinner之类的东西时会有所帮助,例如,因为当我看到此名称时,我知道这是我指定为选择卡车的Spinner

10-08 04:49