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

时间:2022-02-19 20:48:19

This is my first time but I am currently working on a Web Browser and have been trying to implement a Spinner. it has failed and when I try the app will crash so I decided to try here. I have created a new project to the spinner and even that does not work... it says "spinnerHelp has Crashed"

这是我的第一次,但我目前正在使用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 个解决方案

#1


2  

Change

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

Instead

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

Try this in mainactivity.class

在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);


    }


}

#1


2  

Change

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

Instead

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

Try this in mainactivity.class

在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);


    }


}