如何修复我的微调器并继续前进

时间:2017-12-21 02:22:59

标签: java android spinner

这是我的第一次,但我目前正在使用Web浏览器并且一直在尝试实现Spinner。它失败了,当我尝试应用程序将崩溃所以我决定尝试这里。我已经为微调器创建了一个新项目,即使这样也行不通......它说“spinnerHelp已经崩溃了”

MainActivity.java

'package com.equiware.mickeyt.spinnerhelp;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Spinner;
import android.widget.ArrayAdapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;



public class MainActivity extends Activity {
private Spinner spinner;

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


    spinner.setOnItemSelectedListener((OnItemSelectedListener) this);

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
layout
    ArrayAdapter<CharSequence> adapter = 
ArrayAdapter.createFromResource(this,
            R.array.planets_array, 
android.R.layout.simple_spinner_item);


'adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_i
tem);
    spinner.setAdapter(adapter);'

}
public void onItemSelected(AdapterView<?> parent, View view,
                           int pos, long id) {
    // An item was selected. You can retrieve the selected item using
    // parent.getItemAtPosition(pos)
}

public void onNothingSelected(AdapterView<?> parent) {
    // Another interface callback
}


}'

styles.xml

'<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<string-array name="planets_array">
    <item>Mercury</item>
    <item>Venus</item>
    <item>Earth</item>
    <item>Mars</item>
    <item>Jupiter</item>
    <item>Saturn</item>
    <item>Uranus</item>
    <item>Neptune</item>
</string-array>

</resources>'

activity_main.xml中

'<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.equiware.mickeyt.spinnerhelp.MainActivity">

<Spinner
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="487dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>'

1 个答案:

答案 0 :(得分:2)

更改

    spinner = (Spinner) findViewById(R.id.spinner);
    spinner.setOnItemSelectedListener((OnItemSelectedListener) this);

相反

    spinner.setOnItemSelectedListener((OnItemSelectedListener) this);
    Spinner spinner = (Spinner) findViewById(R.id.spinner);

mainactivity.class

中试试
 public class MainActivity extends AppCompatActivity {

    private Spinner spinner;

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


        spinner = (Spinner) findViewById(R.id.spinner);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            }

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

            }
        });


        ArrayAdapter<CharSequence> adapter =
                ArrayAdapter.createFromResource(this,
                        R.array.planets_array,
                        android.R.layout.simple_spinner_item);


        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);


    }


}