尝试运行我的应用程序时遇到以下意外问题。 基本上我正在创建一个调度系统,用户可以在其中选择日历日期,并从微调器下拉选项(早上,下午,晚上)中选择一天中的时间,这些信息将显示在他们尊重的文本视图中。尝试这样做时遇到以下错误:
cannot be cast to android.widget.AdapterView$OnItemSelectedListener
同样由于某种原因,我的微调器没有显示在应用程序中,当它在eclipse布局预览中可见时,所以我想知道它的可见性是否与此代码的错误直接相关。
以下是logcat消息:
09-18 02:27:36.881: E/AndroidRuntime(5871): FATAL EXCEPTION: main
09-18 02:27:36.881: E/AndroidRuntime(5871): Process: com.dooba.beta, PID: 5871
09-18 02:27:36.881: E/AndroidRuntime(5871): java.lang.ClassCastException: com.dooba.beta.ScheduleMatchOptionActivity cannot be cast to android.widget.AdapterView$OnItemSelectedListener
09-18 02:27:36.881: E/AndroidRuntime(5871): at com.dooba.beta.ScheduleMatchOptionActivity.onClick(ScheduleMatchOptionActivity.java:132)
09-18 02:27:36.881: E/AndroidRuntime(5871): at android.view.View.performClick(View.java:4438)
09-18 02:27:36.881: E/AndroidRuntime(5871): at android.view.View$PerformClick.run(View.java:18422)
09-18 02:27:36.881: E/AndroidRuntime(5871): at android.os.Handler.handleCallback(Handler.java:733)
09-18 02:27:36.881: E/AndroidRuntime(5871): at android.os.Handler.dispatchMessage(Handler.java:95)
09-18 02:27:36.881: E/AndroidRuntime(5871): at android.os.Looper.loop(Looper.java:136)
09-18 02:27:36.881: E/AndroidRuntime(5871): at android.app.ActivityThread.main(ActivityThread.java:5017)
09-18 02:27:36.881: E/AndroidRuntime(5871): at java.lang.reflect.Method.invokeNative(Native Method)
09-18 02:27:36.881: E/AndroidRuntime(5871): at java.lang.reflect.Method.invoke(Method.java:515)
09-18 02:27:36.881: E/AndroidRuntime(5871): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
09-18 02:27:36.881: E/AndroidRuntime(5871): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
09-18 02:27:36.881: E/AndroidRuntime(5871): at dalvik.system.NativeStart.main(Native Method)
以下是活动代码:
public class ScheduleMatchOptionActivity extends Activity implements
OnClickListener {
// Widget GUI
protected TextView mCalendarDate;
protected TextView mCalendarTime;
Button btnCalendar, btnTimePicker;
TextView txtDate, txtTime;
// Variable for storing current date and time
private int mYear, mMonth, mDay, mHour, mMinute;
private Spinner spinner;
private static final String[]paths = {"Morning", "Afternoon", "Evening"};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.match_schedule);
txtDate = (TextView) findViewById(R.id.txtDate);
txtTime = (TextView) findViewById(R.id.txtTime);
Button mConfirm2 = (Button)findViewById(R.id.btnConfirmSchedule);
mConfirm2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ParseUser currentUser = ParseUser.getCurrentUser();
// Create the class and the columns
currentUser.saveInBackground();
currentUser.put("ActivityDate", txtDate.getText().toString());
currentUser.put("ActivityTime", txtTime.getText().toString());
currentUser.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
setProgressBarIndeterminateVisibility(false);
if (e == null) {
// Success!
Intent intent = new Intent(ScheduleMatchOptionActivity.this, PayPalPaymentActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(ScheduleMatchOptionActivity.this);
builder.setMessage(e.getMessage())
.setTitle(R.string.signup_error_title)
.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.show();
}
}
});
}
});
btnCalendar = (Button) findViewById(R.id.btnCalendar);
txtDate = (TextView) findViewById(R.id.txtDate);
txtTime = (TextView) findViewById(R.id.txtTime);
btnCalendar.setOnClickListener(this);
btnTimePicker.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == btnCalendar) {
// Process to get Current Date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
// Launch Date Picker Dialog
DatePickerDialog dpd = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
// Display Selected date in textbox
txtDate.setText(dayOfMonth + "-"
+ (monthOfYear + 1) + "-" + year);
}
}, mYear, mMonth, mDay);
dpd.show();
}
spinner = (Spinner)findViewById(R.id.spinTime);
ArrayAdapter<String>adapter = new ArrayAdapter<String>(ScheduleMatchOptionActivity.this,
android.R.layout.simple_spinner_item,paths);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener((OnItemSelectedListener) this);
}
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
switch (position) {
case 0:
txtTime.setText("Morning");
break;
case 1:
txtTime.setText("Afternoon");
break;
case 2:
txtTime.setText("E");
break;
}
}
}
以下是布局部分:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:gravity="center"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</TextView>
<TextView
android:id="@+id/tAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/linearLayout1"
android:layout_gravity="center_horizontal"
android:layout_toLeftOf="@+id/btnCalendar"
android:paddingBottom="25dp"
android:paddingTop="5dp"
android:text="Date and Time Picker Dialog"
android:textColor="#fff"
android:textSize="20sp" >
</TextView>
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout2">
</RelativeLayout>
<TextView
android:id="@+id/txtTime"
android:layout_width="155dp"
android:layout_height="40dp"
android:layout_alignBottom="@+id/spinTime"
android:layout_alignParentLeft="true"
android:layout_marginLeft="16dp"
android:layout_weight="1"
android:ems="10"
android:gravity="center"
android:hint="Select your time"
android:textColor="#C11B17"
android:textSize="18sp" />
<RelativeLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btnCalendar"
android:layout_alignParentLeft="true"
android:layout_alignRight="@+id/tAge" >
<TextView
android:id="@+id/txtDate"
android:layout_width="155dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_weight="1"
android:gravity="center"
android:hint="Select your date"
android:textColor="#C11B17"
android:textSize="18sp" >
<requestFocus />
</TextView>
</RelativeLayout>
<Spinner
android:id="@+id/spinTime"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#D1D0CE"
android:layout_alignLeft="@+id/btnCalendar"
android:layout_below="@+id/btnCalendar"
android:layout_marginTop="17dp" />
<Button
android:id="@+id/btnCalendar"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#D1D0CE"
android:layout_below="@+id/linearLayout2"
android:layout_toRightOf="@+id/txtTime"
android:layout_weight="0"
android:text="Calendar" />
<Button
android:id="@+id/btnConfirmSchedule"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="@+id/txtTime"
android:layout_centerHorizontal="true"
android:layout_marginTop="28dp"
android:alpha="0.8"
android:background="#C11B17"
android:text="Confirm"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
如果您需要进一步说明,请与我们联系。 提前谢谢。
答案 0 :(得分:2)
您的班级必须实施OnItemSelectedListener
public class ScheduleMatchOptionActivity extends Activity implements
OnClickListener, OnItemSelectedListener