简体   繁体   中英

App crashes when try setOnItemSelectedListener(this);

Hi Guys I am working on an app. I want the use to select his/her birthdate from spinners. I will change the number of days depending on the spinner3. If user selects a month which has 30 days my app needs to set the spinner 4 to only contain numbers upto 30. I need to this setting when the user selects an item of spinner3. But my app crashes when I open it. It doesnt crash if I comment out this line: spinner3.setOnItemSelectedListener(this); (solved this)

new problem: It runs onItemSelected(AdapterView parent, View view, int pos, long id) only ones

Hope you find the problem. Thanks

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>

EDIT: Add 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.

Edit: Add ANSWER

I cant answer my question. Please add this ass an answer:

Changing if statement here:

    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);
        }
    }

to

        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);
            }
        }

that and declaring spinners inside methods solved the problem. Thanks everyone who helped.

You can't initialize your Views outside of a method. This will try to initialize them before the layout has been set which means they will be null since they exist in your layout . Change it to

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);

Off-topic At some point, you will want to learn to give your View s and other variables more specific, meaningful names. You have 3 Spinners and knowing which actually does what will be helpful if they mean something like, truckSpinner , for instance because when I see this name I know this is the Spinner I designated as the one to choose a truck.

Initialize your spinners after the setContentView() in onCreate() else you will get NullPointerException-

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM