尝试setOnItemSelectedListener(this)时应用程序崩溃;

时间:2014-01-23 16:24:16

标签: java android

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

新问题:它只运行onItemSelected(AdapterView父,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);
            }
        }

并在方法中声明微调器解决了这个问题。谢谢所有帮助过的人。

2 个答案:

答案 0 :(得分:4)

您无法在方法之外初始化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选择卡车的人。

答案 1 :(得分:3)

在onCreate()中的setContentView()之后初始化你的微调器,否则你会得到NullPointerException -

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