【问题标题】:Something is crashing my app after I implemented Spinner setOnItemSelectedListener实现 Spinner setOnItemSelectedListener 后,我的应用程序崩溃了
【发布时间】:2015-02-03 00:32:09
【问题描述】:

首先要澄清一下,我是 Java 和 Android 的新手,我仍然处于繁重的学习过程中。

无论如何...

我正在开发一个简单的停车短信应用程序,但我被 Spinner 和 TextView 困住了。

基本上,我正在尝试这样做:当我从 Spinner 中选择一个停车区(它有五个停车区)时,我想将停车区描述填充到布局中 Spinner 下方的 TextView 中。

Spinner 有自己的 StringArray,而停车区描述都在自己的 Strings 中。

所以它看起来像这样:

activity_glavni.xml

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

<ScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/scrollView" >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"></LinearLayout>
        </ScrollView>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:text="@string/txGrad"
                android:id="@+id/txOdaberiteGrad"
                android:layout_alignParentTop="true"
                android:layout_alignParentStart="true" />

            <Spinner
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/spinnergradovi"
                android:layout_below="@+id/txOdaberiteGrad"
                android:layout_alignParentStart="true"
                android:layout_marginTop="23dp" />
            <Space
                android:layout_width="20px"
                android:layout_height="20px"
                android:id="@+id/space" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:id="@+id/txZona"
                android:text="@string/txZona"
                android:layout_below="@+id/spinnergradovi"
                android:layout_alignParentStart="true" />


            <Spinner
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/spinnerZona"
                android:layout_below="@+id/txZona"
                android:layout_alignParentStart="true"
                android:layout_marginTop="23dp" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:id="@+id/txPrikazZone"
                android:layout_below="@+id/spinnerZona" />

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/editTextRegistracija"
    android:layout_below="@+id/txPrikazZone"
    android:layout_alignParentStart="true"
    android:hint="Unesite broj registracije" />

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/btn_posalji"
    android:id="@+id/btn_posalji"
    android:layout_below="@+id/editTextRegistracija"
    android:layout_alignParentStart="true"
    android:layout_marginTop="33dp"
    android:layout_alignParentEnd="false"
    />

字符串.xml

<resources>
<string name="app_name">ParkingZagreb</string>


<string name="action_settings">Settings</string>
<string name="txGrad">Odaberite grad</string>
<string name="txZona">Odaberite zonu</string>
<string name="btn_posalji">Pošalji SMS!</string>
<string-array name="gradovi">
    <item>Zagreb</item>
    <item>Velika Gorica</item>
    <item>Zaprešić</item>
</string-array>


<string-array name="zone">
    <item>Prva zona</item>
    <item>Druga zona</item>
    <item>Treća zona</item>
    <item>Četvrta zona (1)</item>
    <item>Četvrta zona (2)</item>
</string-array>

<string-array name="opisi_zona">
    <item>6 kn/h, maksimalno 2 h</item>
    <item>3 kn/h, maksimalno 3 h</item>
    <item>1,5 kn/h, bez ograničenja</item>
    <item>5 kn/dan, 7-16 h</item>
    <item>10 kn/dan, 7-20 h</item>
</string-array>

Glavni.java

public class Glavni extends ActionBarActivity {

public TextView txPrikazZone;
private Button btn_posalji;


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

    this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    btn_posalji = (Button) findViewById(R.id.btn_posalji);
    btn_posalji.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            AlertDialog alertDialog = new AlertDialog.Builder(Glavni.this).create();
            alertDialog.setTitle("SMS poslan!");
            alertDialog.setMessage("Provjerite SMS aplikaciju. Poruka bi trebala doći za nekoliko trenutaka.");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {


                }
            });

            alertDialog.show();
        }
    });


    // the first city selection Spinner


    Spinner spinner_gradovi = (Spinner) findViewById(R.id.spinnergradovi);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.gradovi, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(
            android.R.layout.simple_spinner_dropdown_item);
    spinner_gradovi.setAdapter(adapter);


    // the second Spinner with parking zones

    final Spinner spinner_zona = (Spinner) findViewById(R.id.spinnerZona);
    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
            this, R.array.zone, android.R.layout.simple_spinner_item);
    adapter2.setDropDownViewResource(
            android.R.layout.simple_spinner_dropdown_item);
    spinner_zona.setAdapter(adapter2);


    // this is where I'm trying to populate the TextView with the String-Array by selecting an item from the second Spinner     

    final String[] opisi_zona = getResources().getStringArray(R.array.opisi_zona);

    spinner_zona.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            txPrikazZone.setText(opisi_zona[position]);
        }

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

        }
    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_glavni, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

感谢您的帮助...

【问题讨论】:

  • txPrikazZone 是否已正确初始化?
  • @IgorGajic Glavni.java 的第 89 行是什么?
  • @stkent 设置为公开。
  • @iRuth 它是 txPrikazZone.setText(zona1);
  • 请发布完整的源代码,显示您在哪里初始化txPrikazZone

标签: android crash textview spinner


【解决方案1】:

空指针的原因是你没有初始化txPrikazZone,你正在尝试调用setText

您需要在您的 onCreate 方法中初始化它,如下所示:

txPrikazZone = (TextView) findViewById(R.id.txPrikazZone);

我希望这会有所帮助。

【讨论】:

  • 哦,天哪,基础知识...我为自己感到羞耻哈哈。好吧,正如我所说,我是新手。非常感谢您的宝贵时间和建议。
【解决方案2】:

你的适配器对于两个 Spinner 都是一样的

编辑

首先尝试将 ZonaX 字符串包装到字符串数组中

<string-array name="zonas">
     ...
</string-array>

现在在代码中更改 ItemSelectedListener 的集合

final String[] zonas = getResources().getStringArray(R.array.zonas); 

spinner_zona.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        txPrikazZone.setText(zonas[position]);
    }

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

    }
}

【讨论】:

  • 我刚刚为第二个 Spinner 重命名了适配器,但它仍然崩溃。
  • 感谢您的建议。我不知道我做错了什么,但它不会让我选择“getStringArray”。我只能选择“getString”,它不起作用。我也试过 getResources ,但没有运气。它在它下划线并说“将变量...更改为 java.lang.String。”
  • 对不起,我忘记了 getStringArray() 之前的 getResources(),再次检查我的编辑。
  • 非常感谢,但仍然没有运气。它仍然在崩溃。看起来,它在“at com.example.igor.parkingzagreb.Glavni$2.onItemSelected(Glavni.java:87)”上设置了一个错误,即“txPrikazZone.setText(opisi_zona[position]);”。我在布局中检查了 TextView。它的“文本”属性为空(因为应该从字符串数组中提取文本),宽度设置为 match_parent...我不知道...
  • 在设置值之前检查 txPrikazZone 是否为空。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多