【问题标题】:Error creating ArrayAdapter创建 ArrayAdapter 时出错
【发布时间】:2016-09-18 18:22:08
【问题描述】:

我正在尝试将适配器设置为微调器,但我不知道为什么会出现以下错误:

无法解析构造函数 'ArrayAdapter(com.example.app.DialogBox,int, java.util.list)'

这是我尝试填充微调器并为其设置适配器的方法:

public class DialogBox extends DialogFragment implements View.OnClickListener {

     // To fill the age spinner
    public ArrayAdapter<Integer> populateAgeSpinner () {

        Log.d(TAG ,"populateAgeSpinner - Ini");

        List age =  new ArrayList<String>();
        age.add("");

        for(int i =  18; i <= 100; i++) {
            age.add(Integer.toString(i));
        }

        ArrayAdapter<Integer> spinnerAgeAdapter = new ArrayAdapter<Integer>(this,R.layout.spinner_item_layout, age);
        spinnerAgeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        Log.d(TAG ,"populateAgeSpinner - Fi");

        return spinnerAgeAdapter;

    }
}

这是spinner_item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/spinner_text"
        style="?android:attr/spinnerItemStyle"
        android:singleLine="true"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:ellipsize="marquee"
        android:background="@color/com_facebook_button_send_background_color"
        android:textColor="#000"
        android:textAlignment="inherit"/>

【问题讨论】:

    标签: android android-fragments android-arrayadapter


    【解决方案1】:

    DialogFragment 不扩展每个ArrayAdapter 构造函数所需的Context。您应该在构造函数中使用Context 或其任何继承者。要获取Context,请调用FragmentgetActivity() 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      • 1970-01-01
      • 2012-12-16
      • 2012-07-07
      • 2014-07-24
      • 2017-07-16
      相关资源
      最近更新 更多