【问题标题】:Custom Spinner Crashing自定义微调器崩溃
【发布时间】:2023-03-14 07:03:01
【问题描述】:

大约一周前,我在这里问了一个关于自定义微调器的问题,并被引导到本指南。 http://app-solut.com/blog/2011/03/using-custom-layouts-for-spinner-or-listview-entries-in-android/

我遵循它,并尝试对其进行调整以与我的代码一起使用并将结果从数据库拉到微调器上,但它一直在崩溃。

这是微调器的代码。

public class EditTeam extends Activity {
private final List<SpinnerEntry> spinnerContent = new LinkedList<SpinnerEntry>();
private Spinner D1Spinner;
private final ETSpinnerAdapter D1Adapter = new ETSpinnerAdapter(spinnerContent, this);
DataBaseHelper myDbHelper = new DataBaseHelper(this);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.editteam);

    myDbHelper = new DataBaseHelper(this);
    myDbHelper.openDataBase();
    fillSpinner();

}

private void fillSpinner() {
    Cursor c = myDbHelper.FetchDrivers();
    startManagingCursor(c);

    // create an array to specify which fields we want to display
    String[] from = new String[]{"FirstName", "LastName"};
    // create an array of the display item we want to bind our data to
    int[] to = new int[]{android.R.id.text1};


    spinnerContent.add(new SpinnerEntry(1, null, "Test"));



    //adapter.setDropDownViewResource( R.layout.spinner_entry_with_icon );

    D1Spinner = (Spinner) findViewById(R.id.spr_Driver1);
    D1Spinner.setAdapter((SpinnerAdapter) D1Adapter);
}

}

我正在使用该联系人示例中的两个类,目前尚未修改。

如您所见,我目前正在尝试手动添加一项,但加载时它会崩溃。

这似乎是突破点?

05-25 15:17:34.773:E/AndroidRuntime(241):java.lang.RuntimeException:无法启动活动 ComponentInfo{com.f1manager.android/com.f1manager.android.EditTeam}:java.lang。 ClassCastException: com.f1manager.android.ETSpinnerAdapter

任何想法都会很棒。

谢谢。

ETSpinnerAdapter 代码(未修改示例中的原始代码):

public class ETSpinnerAdapter {
private final List<SpinnerEntry> content;
private final Activity activity;

public ETSpinnerAdapter(List<SpinnerEntry> content, Activity activity) {
super();
this.content = content;
this.activity = activity;
}

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

public SpinnerEntry getItem(int position) {
return content.get(position);
}

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

public View getView(int position, View convertView, ViewGroup parent) {
final LayoutInflater inflater = activity.getLayoutInflater();
final View spinnerEntry = inflater.inflate(
R.layout.spinner_entry_with_icon, null);    // initialize the layout from xml
final TextView contactName = (TextView) spinnerEntry
        .findViewById(R.id.spinnerEntryContactName);
final ImageView contactImage = (ImageView) spinnerEntry
.findViewById(R.id.spinnerEntryContactPhoto);
final SpinnerEntry currentEntry = content.get(position);
contactName.setText(currentEntry.getContactName());
//contactImage.setImageBitmap(currentEntry.getContactPhoto());
return spinnerEntry;
}

}

【问题讨论】:

  • 在此处添加完整的 logcat 和布局文件
  • 我认为问题出在:D1Spinner.setAdapter((SpinnerAdapter) D1Adapter) 因为当某些转换失败时记录错误。
  • 如果我按照指南上的方式进行操作,我会收到此错误:如果我拿走 (SpinnerAdapter),则 AbsSpinner 类型中的方法 setAdapter(SpinnerAdapter) 不适用于参数 (ETSpinnerAdapter)

标签: android sqlite spinner


【解决方案1】:

您的 ETSpinnerAdapter 似乎不是 SpinnerAdapter,因为您正在获得一个类强制转换异常。也许您可以发布 ETSpinnerAdapter 的代码?

【讨论】:

  • 感谢您的回复,我已经使用未修改联系人示例的 ETSpinnerAdapter 代码更新了原始代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-13
相关资源
最近更新 更多