getSelectedItemPosition

getSelectedItemPosition

我创建了一个带有自定义适配器的微调器,他看起来是这样的。
android - 微调框getSelectedItem()引发异常-LMLPHP
但是,在我的活动中,当我按下save按钮时,我试图getSelectedItem()getSelectedItemPosition(),抛出一个错误。
当执行getSelectedItem()twrow aNullPointerException错误时,当执行getSelectedItemPosition()时,错误为Resources$NotFoundException: String resource ID #0x7
遵循我的活动、微调器行和微调器适配器的代码
fishformactivity.java

    package br.com.vitorvillar.aquariummanager.screens;

    import android.os.Bundle;
    import android.app.Activity;
    import android.util.Log;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.Spinner;
    import android.widget.Toast;

    import br.com.vitorvillar.aquariummanager.R;
    import br.com.vitorvillar.aquariummanager.adapters.FishesSpinnerAdapter;
    import br.com.vitorvillar.aquariummanager.libraries.FishTypeMap;

    public class FishFormActivity extends Activity {

        private Spinner mSpinner;
        private EditText mAmount;

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

            /** Get the The fields */
            this.mSpinner = (Spinner) this.findViewById(R.id.spinnerFishSpecies);
        this.mAmount = (EditText) this.findViewById(R.id.txtAmount);

        /** Get the FishMap */
        FishTypeMap fishType = new FishTypeMap(this);
        fishType.loadFreshWaterMap();

        /** Create and set the adapter */
        FishesSpinnerAdapter adapter = new FishesSpinnerAdapter(this, fishType);
    this.mSpinner.setAdapter(adapter);

}

public void saveFish(View view) {
    Toast.makeText(this, this.mSpinner.getSelectedItem(), Toast.LENGTH_LONG).show();
}

}
fishesspinneradapter.java网站
package br.com.vitorvillar.aquariummanager.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;

import java.util.List;

import br.com.vitorvillar.aquariummanager.R;
import br.com.vitorvillar.aquariummanager.libraries.FishTypeMap;

public class FishesSpinnerAdapter extends BaseAdapter {

    private List<String> mNames;
    private List<String> mScientificNames;
    private List<Integer> mImages;
    private LayoutInflater mLayoutInflater;


    public FishesSpinnerAdapter(Context context, FishTypeMap fishType) {

        /** Get the values from the Fish type */
        this.mNames = fishType.getNames();
        this.mScientificNames = fishType.getScientificNames();
        this.mImages = fishType.getImages();

        /** Get the inflater layout */
        this.mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getCount() {
        return this.mImages.size();
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = this.mLayoutInflater.inflate(R.layout.fishes_spinner_item, parent, false);
            holder = new ViewHolder();
            holder.mFishName = (TextView) convertView.findViewById(R.id.txtFishName);
            holder.mFishScientificName = (TextView) convertView.findViewById(R.id.txtFishScientificName);
            holder.mFishImage = (RoundedImageView) convertView.findViewById(R.id.fishIcon);

            /** Set the holder as tag */
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.mFishName.setText(this.mNames.get(position));
        holder.mFishScientificName.setText(this.mScientificNames.get(position));
        holder.mFishImage.setImageResource(this.mImages.get(position));


        return convertView;
    }

    public static class ViewHolder {
        public RoundedImageView mFishImage;
        public TextView mFishName;
        public TextView mFishScientificName;
    }

}

fishes_spinner_item.xml鱼
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingStart="10dp">

    <com.makeramen.roundedimageview.RoundedImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/fishIcon"
        android:layout_width="60dp"
        android:layout_height="70dp"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_attention"
        app:riv_border_color="#666"
        app:riv_border_width="1dp"
        app:riv_corner_radius="10dp"
        tools:ignore="ContentDescription" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:paddingStart="10dp"
    android:paddingTop="12dp">

    <TextView
        android:id="@+id/txtFishName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Molinésia"
        android:textStyle="bold" />

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:ignore="UselessParent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/txtFishScientificName"
                android:textColor="@android:color/darker_gray" />

        </TableRow>

    </TableLayout>
</LinearLayout>

下面是执行getSelectedItem()时的错误:
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: FATAL EXCEPTION: main
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: Process: br.com.vitorvillar.aquariummanager, PID: 1614
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime: java.lang.IllegalStateException: Could not execute method of the activity
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at android.view.View$1.onClick(View.java:4007)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at android.view.View.performClick(View.java:4756)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:19761)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5253)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:  Caused by: java.lang.reflect.InvocationTargetException
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at android.view.View$1.onClick(View.java:4002)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at android.view.View.performClick(View.java:4756) 
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:19761) 
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739) 
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95) 
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5253) 
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at br.com.vitorvillar.aquariummanager.screens.FishFormActivity.saveFish(FishFormActivity.java:53)
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372) 
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at android.view.View$1.onClick(View.java:4002) 
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at android.view.View.performClick(View.java:4756) 
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:19761) 
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:739) 
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95) 
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135) 
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5253) 
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
04-05 13:02:18.997 1614-1614/br.com.vitorvillar.aquariummanager E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)

我创建的适配器没有扩展ArrayAdapter,这可能是问题所在吗?但在执行时引发错误是没有意义的getSelectedItemPosition()
注意:当我在getSelectedItemPosition()方法内执行onCreate()时,返回位置0。
有人能帮我吗?

最佳答案

执行getselecteditemposition()时,错误为resources$notfoundexception:string resource id 0x7
这是因为getSelectedItemPosition()的返回类型是一个int-所以makeText()方法认为它是一个字符串资源id。要正确显示位置,请使用String.valueOf(getSelectedItemPosition())作为makeText()方法的第二个参数。
当执行getselecteditem()twrow时出现nullpointerexception错误
从stacktrace看来,您试图在未选择任何项时调用toString()上的getSelectedItem(),因此我的建议是在调用getSelectedItem() != null之前检查是否makeText()
另一个观察结果是,您在适配器中返回null作为getItem()。更改为return mNames.get(position);
在oncreate()方法中执行getselecteditemposition()时,返回位置0
由于尚未选择项目-getSelectedItemPosition()将返回默认值0。

10-07 19:22