我有一个通过调用以下xml来设置xml的活动。此xml布局依次调用一个自定义的复合组件,该组件设置了两个按钮和一个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)
最佳答案
似乎您正在尝试在以下代码行上将类型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);