java.lang.ClassCastException:在自定义复合组件上

时间:2012-05-08 19:31:03

标签: java android xml

我有一个通过调用下面的xml来设置xml的活动。此xml布局inturn调用自定义复合组件,该组件设置两个按钮和一个edittext。然后,我的主要活动试图从自定义复合组件定义的编辑文本中获取值。我有下面的logcat报告,但看不出它是什么我没有得到正确的。

XML布局文件

        <com.android.control.MinutePicker
            android:id="@+id/MinPick"
            android:orientation="vertical"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </com.android.control.MinutePicker>

com.android.control.MinutePicker

but1 = new Button( context );
but1.setTextSize( TEXT_SIZE );
but1.setText( "-" );
but2 = new Button( context );
but2.setTextSize( TEXT_SIZE );
but2.setText( "-" );
valueText = new EditText( context );
valueText.setTextSize( TEXT_SIZE );
valueText.setGravity( Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL );
valueText.setText(0);
this.setLayoutParams( new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT ) );
LayoutParams elementParams = new LinearLayout.LayoutParams( ELEMENT_WIDTH, ELEMENT_HEIGHT );

    addView( but1, elementParams );
    addView( valueText, elementParams );
    addView( but2, elementParams );

然后尝试获取编辑文本值的主要活动。

             EditText Identifier = (EditText)findViewById(R.id.MinPick);
             String value = Identifier.getText().toString();
             Toast toast = Toast.makeText(PerfectSleepActivity.this, value, Toast.LENGTH_SHORT);
             toast.setGravity(Gravity.CENTER, 0, 0);
             toast.show(); 

LogCat报告

05-08 19:03:56.141: E/AndroidRuntime(200): Uncaught handler: thread main exiting due to uncaught exception
05-08 19:03:57.730: E/AndroidRuntime(200): java.lang.ClassCastException: com.android.control.MinutePicker
05-08 19:03:57.730: E/AndroidRuntime(200):  at com.ap.df.DFActivity$2.onClick(PerfectSleepActivity.java:37)
05-08 19:03:57.730: E/AndroidRuntime(200):  at android.view.View.performClick(View.java:2344)
05-08 19:03:57.730: E/AndroidRuntime(200):  at android.view.View.onTouchEvent(View.java:4133)
05-08 19:03:57.730: E/AndroidRuntime(200):  at android.widget.TextView.onTouchEvent(TextView.java:6510)
05-08 19:03:57.730: E/AndroidRuntime(200):  at android.view.View.dispatchTouchEvent(View.java:3672)
05-08 19:03:57.730: E/AndroidRuntime(200):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
05-08 19:03:57.730: E/AndroidRuntime(200):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
05-08 19:03:57.730: E/AndroidRuntime(200):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
05-08 19:03:57.730: E/AndroidRuntime(200):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
05-08 19:03:57.730: E/AndroidRuntime(200):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
05-08 19:03:57.730: E/AndroidRuntime(200):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
05-08 19:03:57.730: E/AndroidRuntime(200):  at android.app.Activity.dispatchTouchEvent(Activity.java:1987)
05-08 19:03:57.730: E/AndroidRuntime(200):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
05-08 19:03:57.730: E/AndroidRuntime(200):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
05-08 19:03:57.730: E/AndroidRuntime(200):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-08 19:03:57.730: E/AndroidRuntime(200):  at android.os.Looper.loop(Looper.java:123)
05-08 19:03:57.730: E/AndroidRuntime(200):  at android.app.ActivityThread.main(ActivityThread.java:4203)
05-08 19:03:57.730: E/AndroidRuntime(200):  at java.lang.reflect.Method.invokeNative(Native Method)
05-08 19:03:57.730: E/AndroidRuntime(200):  at java.lang.reflect.Method.invoke(Method.java:521)
05-08 19:03:57.730: E/AndroidRuntime(200):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
05-08 19:03:57.730: E/AndroidRuntime(200):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
05-08 19:03:57.730: E/AndroidRuntime(200):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

看起来你正试图在这行代码上将类MinutePicker(com.android.control.MinutePicker)类转换为EditText:

EditText Identifier = (EditText)findViewById(R.id.MinPick);

R.id.MinPick的类型为MinutePicker,而不是EditText。

这不起作用。 MinutePicker是继承EditText还是什么?请发布类标识符(而不仅仅是其中包含的代码)。

也许您的意思是执行以下操作:

MinutePicker mp = (MinutePicker)findViewById(R.id.MinPick);