每次OnItemSelectedListener强制关闭应用

时间:2015-09-05 07:20:35

标签: java android android-studio listeners

我正在测试我将在工作应用中实现的某些功能,但是这个问题让我发疯:

我在strings.xml中使用stringn数组写入"下拉式微调器",并使用onItemSelectListener显示所选的条目,但app力量在旋转时关闭。 setOnItemSelectedListener()方法,我完全不知道。

我可以补充说,如果有关于此的教程,我可能已经完成了它并且它没有解决问题。

目标api 22; min api:15

xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/spinner"
    android:spinnerMode="dropdown" />

这是我所做的代码的最后一次更改,试图获取

.java文件:

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public void initializeSpinner()
{
    Spinner spin = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.temp_spinner, support_simple_spinner_dropdown_item);
    adapter.setDropDownViewResource(support_simple_spinner_dropdown_item);
    spin.setAdapter(adapter);
    spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(parent.getContext(),
                    "OnItemSelectedListener : " + parent.getItemAtPosition(position).toString(),
                    Toast.LENGTH_SHORT).show();
        }

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

        }
    });
}

Logcat输出:

09-05 09:10:27.164  24416-24416/com.example.cybex.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.cybex.myapplication, PID: 24416
java.lang.RuntimeException: Failed to resolve attribute at index 6
        at android.content.res.TypedArray.getLayoutDimension(TypedArray.java:603)
        at android.view.ViewGroup$LayoutParams.setBaseAttributes(ViewGroup.java:6474)
        at android.view.ViewGroup$LayoutParams.<init>(ViewGroup.java:6426)
        at android.view.ViewGroup.generateLayoutParams(ViewGroup.java:5364)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
        at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
        at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:197)
        at android.widget.Spinner.onMeasure(Spinner.java:507)
        at android.view.View.measure(View.java:17562)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
        at android.view.View.measure(View.java:17562)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
        at android.view.View.measure(View.java:17562)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
        at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:447)
        at android.view.View.measure(View.java:17562)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2625)
        at android.view.View.measure(View.java:17562)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5891)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
        at android.view.Choreographer.doCallbacks(Choreographer.java:580)
        at android.view.Choreographer.doFrame(Choreographer.java:550)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5281)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:375)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

2 个答案:

答案 0 :(得分:0)

我认为您的ArrayAdapter构造函数是问题所在,您使用的是layout.simple_spinner_item,我认为应该是android.R.layout.simple_spinner_item,请尝试以下操作:

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

答案 1 :(得分:0)

尝试更改适配器布局文件:

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
        this, R.array.temp_spinner, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapter);

spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(parent.getContext(),
                    "OnItemSelectedListener : " + parent.getItemAtPosition(position).toString(),
                    Toast.LENGTH_SHORT).show();
        }

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

        }
    });

example