首先需要说明的是,我是Java和Android的新手,而且我的学习过程仍然很繁重。

无论如何...

我正在开发一个简单的Parking SMS应用程序,并且使用了Spinner和TextView。

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

Spinner有其自己的StringArray,而Parking区域描述均在其自己的String中。

所以看起来像这样:

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"
    />

strings.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,而您正在尝试调用setText

您需要像这样在onCreate方法中对其进行初始化:
txPrikazZone = (TextView) findViewById(R.id.txPrikazZone);
我希望这有帮助。

09-11 10:06