用自定义布局微调框不显示什么 Android

标签: Android
发布时间: 2013/11/26 22:56:15
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

在我的应用程序必须使用微调框应显示它、 EN PR) 的三个项目。我还必须使用个性化的布局。代码是在另一个适配器里面方法中:

holder.spinnerLenguage = (Spinner) convertView
                .findViewById(R.id.spinnerLenguage);
        ArrayList<String> spinnerArray = new ArrayList<String>();
        spinnerArray.add("IT");
        spinnerArray.add("EN");
        spinnerArray.add("PR");
        final lenguageSpinnerAdapter lenguageAdapter = new lenguageSpinnerAdapter(
                convertView.getContext(), spinnerArray);
        holder.spinnerLenguage.setAdapter(lenguageAdapter);

        holder.spinnerLenguage
                .setOnItemSelectedListener(new OnItemSelectedListener()
...
...

适配器类是:

private class lenguageSpinnerAdapter extends ArrayAdapter<String> implements
        SpinnerAdapter {

    private ArrayList<String> lenguages;

    public lenguageSpinnerAdapter(Context context,
            ArrayList<String> lenguages) {
        super(context, R.layout.lenguage_item_layout);
        this.lenguages = lenguages;
    }

    @Override
    public View getDropDownView(int position, View convertView,
            ViewGroup parent) {
        // First let's verify the convertView is not null
        if (convertView == null) {
            // This a new view we inflate the new layout
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.lenguage_item_layout,
                    parent, false);
        }

        TextView lenguage = (TextView) convertView
                .findViewById(R.id.textViewLenguage);
        lenguage.setText(lenguages.get(position).toString());

        return convertView;

    }

}

编辑: 与 getCount 的错误是:

07-11 11:09:20.222: E/AndroidRuntime(32564): FATAL EXCEPTION: main
07-11 11:09:20.222: E/AndroidRuntime(32564): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app/com.app.FlipMainActivity}: java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.app.ActivityThread.access$700(ActivityThread.java:152)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.os.Looper.loop(Looper.java:137)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.app.ActivityThread.main(ActivityThread.java:5328)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at java.lang.reflect.Method.invokeNative(Native Method)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at java.lang.reflect.Method.invoke(Method.java:511)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at dalvik.system.NativeStart.main(Native Method)
07-11 11:09:20.222: E/AndroidRuntime(32564): Caused by: java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
07-11 11:09:20.222: E/AndroidRuntime(32564):    
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.widget.Spinner.onMeasure(Spinner.java:443)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.view.View.measure(View.java:16059)
071 11:09:20.222: E/AndroidRuntime(32564):  at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.view.View.measure(View.java:16059)
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
...
...
07-11 11:09:20.222: E/AndroidRuntime(32564): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
07-11 11:09:20.222: E/AndroidRuntime(32564):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
07-11 11:09:20.222: E/AndroidRuntime(32564):    ... 29 more

微调按钮是在布局中绘制,但没有任何项目。有什么想法?

解决方法 1:

尝试执行以下代码而不是你的人。

private class lenguageSpinnerAdapter extends BaseAdapter implements
        SpinnerAdapter {

    private ArrayList<String> lenguages;

    public lenguageSpinnerAdapter(Context context,
            ArrayList<String> lenguages) {
        this.lenguages = lenguages;
    }

    public int getCount() {
        return lenguages.size();
    }

    public Object getItem(int position) {
        return lenguages.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            // This a new view we inflate the new layout
            LayoutInflater inflater = (LayoutInflater) getApplicationContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.get_exercise_activity,
                    parent, false);
        }

        TextView lenguage = (TextView) convertView
                .findViewById(R.id.btnSave);
        lenguage.setText(lenguages.get(position).toString());

        return convertView;
    }

}
赞助商