gpt4 book ai didi

java - 如何将包装类附加到 "view"类型以外的 View

转载 作者:行者123 更新时间:2023-11-29 21:05:19 26 4
gpt4 key购买 nike

我想将一个新类附加到以 XML 定义的 ImageView。以下代码有效,但仅适用于 View 类型。当我如下所示将类型更改为 ImageView 时,我收到了 ClassCastException。

这是我的 article_view.xml 文件:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bg_color"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="600">


<ImageView
android:id="@+id/mybutton1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="75"
class="com.example.customui2.MainActivity$ArticleFragment$MyButton"
android:contentDescription="@string/logo_desc"
android:src="@drawable/menu_button"
android:text="@string/button1"
android:textColor="@color/button1_color"
android:textSize="45sp" />

</LinearLayout>

</RelativeLayout>

和这样的源文件:

public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.fragment_container) != null) {

// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}

// Create a new Fragment to be placed in the activity layout
ArticleFragment firstFragment = new ArticleFragment();

// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
firstFragment.setArguments(getIntent().getExtras());

// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, firstFragment).commit();
}

}

public static class ArticleFragment extends Fragment {

// Animation
Animation animFadein;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.article_view, container, false);

MyButton button1 = (MyButton) rootView.findViewById(R.id.mybutton1);

// load the animation
animFadein = AnimationUtils.loadAnimation(rootView.getContext().getApplicationContext(),
R.anim.fade_in);

button1.startAnimation(animFadein);

return rootView;
}

public static class MyButton extends View implements
{
Bitmap backgroundImage;
Paint backgroundPaint;
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
backgroundImage = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
backgroundPaint = new Paint();
}

@Override
protected void onDraw(Canvas canvas)
{
canvas.drawBitmap(backgroundImage, 0, 0, backgroundPaint);
super.onDraw(canvas);
}

};
}
}

现在我在这一行中收到错误 ClassCastException:

MyButton button1 = (MyButton) rootView.findViewById(R.id.mybutton1);

这是 logcat:

07-16 17:35:36.855: E/AndroidRuntime(7039): FATAL EXCEPTION: main
07-16 17:35:36.855: E/AndroidRuntime(7039): Process: com.example.customui2, PID: 7039
07-16 17:35:36.855: E/AndroidRuntime(7039): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.customui2/com.example.customui2.MainActivity}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to com.example.customui2.MainActivity$ArticleFragment$MyButton
07-16 17:35:36.855: E/AndroidRuntime(7039): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
07-16 17:35:36.855: E/AndroidRuntime(7039): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
07-16 17:35:36.855: E/AndroidRuntime(7039): at android.app.ActivityThread.access$800(ActivityThread.java:135)
07-16 17:35:36.855: E/AndroidRuntime(7039): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
07-16 17:35:36.855: E/AndroidRuntime(7039): at android.os.Handler.dispatchMessage(Handler.java:102)
07-16 17:35:36.855: E/AndroidRuntime(7039): at android.os.Looper.loop(Looper.java:136)
07-16 17:35:36.855: E/AndroidRuntime(7039): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-16 17:35:36.855: E/AndroidRuntime(7039): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 17:35:36.855: E/AndroidRuntime(7039): at java.lang.reflect.Method.invoke(Method.java:515)
07-16 17:35:36.855: E/AndroidRuntime(7039): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-16 17:35:36.855: E/AndroidRuntime(7039): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-16 17:35:36.855: E/AndroidRuntime(7039): at dalvik.system.NativeStart.main(Native Method)
07-16 17:35:36.855: E/AndroidRuntime(7039): Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to com.example.customui2.MainActivity$ArticleFragment$MyButton
07-16 17:35:36.855: E/AndroidRuntime(7039): at com.example.customui2.MainActivity$ArticleFragment.onCreateView(MainActivity.java:110)
07-16 17:35:36.855: E/AndroidRuntime(7039): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1504)
07-16 17:35:36.855: E/AndroidRuntime(7039): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
07-16 17:35:36.855: E/AndroidRuntime(7039): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1121)
07-16 17:35:36.855: E/AndroidRuntime(7039): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-16 17:35:36.855: E/AndroidRuntime(7039): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
07-16 17:35:36.855: E/AndroidRuntime(7039): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:571)
07-16 17:35:36.855: E/AndroidRuntime(7039): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
07-16 17:35:36.855: E/AndroidRuntime(7039): at android.app.Activity.performStart(Activity.java:5241)
07-16 17:35:36.855: E/AndroidRuntime(7039): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
07-16 17:35:36.855: E/AndroidRuntime(7039): ... 11 more
07-16 17:35:39.895: I/Process(7039): Sending signal. PID: 7039 SIG: 9

我应该更改什么才能将类附加到 View 以外的类型,例如 ImageView?

最佳答案

如果要将 MyButton 转换为 ImageView,则必须扩展 ImageView。

如果您的 MyButton 类只是一个 View ,那么它缺少您尝试使用的所有 ImageView 组件。也许您可以改用常规的 ImageView。

编辑:

您可以通过使用小写 View 标签+class 属性在XML 中引用MyButton 类:

<view
android:id="@+id/mybutton1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="75"
class="com.example.customui2.MainActivity$ArticleFragment$MyButton"
android:contentDescription="@string/logo_desc"
android:src="@drawable/menu_button"
android:text="@string/button1"
android:textColor="@color/button1_color"
android:textSize="45sp" />

然后像这样声明你的静态类:

public static class MyButton extends ImageView 

我对其进行了测试,它没有崩溃。

关于java - 如何将包装类附加到 "view"类型以外的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24791513/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com