gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:27:52 28 4
gpt4 key购买 nike

大家好,我正在开发一个应用程序。我希望用户可以从旋转器中选择他/她的生日。我将根据 spinner3 更改天数。如果用户选择有 30 天的月份,我的应用程序需要将 spinner 4 设置为仅包含最多 30 个数字。当用户选择 spinner3 的项目时,我需要此设置。 但是当我打开我的应用程序时它崩溃了。如果我注释掉这一行,它不会崩溃: 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。这将尝试在设置布局之前初始化它们,这意味着它们将为null,因为它们存在于您的布局中。将其更改为

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 个 Spinner,如果它们的意思类似于 truckSpinner,那么知道哪个实际上做了什么将会很有帮助,例如,因为当我看到这个名字时,我知道这是 >Spinner 我指定为选择卡车的人。

关于java - 尝试 setOnItemSelectedListener(this); 时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21313813/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com