gpt4 book ai didi

自定义类中的 Android SurfaceView

转载 作者:行者123 更新时间:2023-11-30 01:20:43 25 4
gpt4 key购买 nike

所以我在 Android Studio 中做的项目中一直遇到 NullPointerException。我需要让图像显示在我称为 DrawSurface 的自定义类中。我似乎找不到与我的问题相关的任何内容。我检查了以前的各种问题并观看了很多视频。

我抛出的错误看起来像这样:

java.lang.NullPointerException   at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1269)   at android.graphics.Canvas.drawBitmap(Canvas.java:1325)   at edu.########.#####.cse.treasurehunter.###########.DrawSurface.onDraw_Original(DrawSurface.java:60)   at edu.########.#####.cse.treasurehunter.###########.DrawSurface.onDraw(DrawSurface.java:-1)

我的 .xml 看起来像这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="fill_parent" android:orientation="vertical" tools:context=".MainActivity">
<edu.########.####.cse.treasurehunter.###########.DrawSurface android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" android:id="@+id/dsField"/>
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="50dp">
<Button android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="0.5" android:text="View Inventory" android:id="@+id/btnInventory"/>
<Button android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="0.5" android:text="Credits" android:id="@+id/btnCredits"/>
</LinearLayout>

我的 DrawSurface 类如下所示:

public class DrawSurface extends SurfaceView {
private Bitmap mBMPField;
private SurfaceHolder holder;

public DrawSurface(Context context) {
super(context);
holder = getHolder();
holder.addCallback(new SurfaceHolder.Callback() {

@Override
public void surfaceCreated(SurfaceHolder holder) {

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Canvas c = holder.lockCanvas();
onDraw(c);
holder.unlockCanvasAndPost(c);
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

}
});
mBMPField = BitmapFactory.decodeResource(getResources(), R.drawable.field);
}

public DrawSurface(Context context, AttributeSet attrs) {
super(context, attrs);
}

public DrawSurface(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.DKGRAY);
canvas.drawBitmap(mBMPField, 100, 200, null);
}

最佳答案

  1. 如果您的目标是在 SurfaceView 的表面(而不是 SurfaceView 的 View )上绘图,请不要覆盖 onDraw()onDraw() 适用于 custom views .如果您真的想使用 Surface,请将该方法命名为其他名称。
  2. 不要只在 surfaceChanged() 中绘制,除非您要发布静态图像。该方法通常会在首次创建 Surface 时被调用一次,除非有什么改变了 Surface 的大小。如果您只想要位图中的静态图像,请考虑改用 ImageView。
  3. 不要假设 lockCanvas() 会成功。从 surfaceChanged() 调用它是非常安全的,但通常您需要检查返回值。
  4. 不要假设 BitmapFactory.decodeResource() 会成功。

由于您的 drawColor() 调用成功,而您的 drawBitmap() 调用失败,我猜测 mBMPField 为空。根据 decodeResource() 的文档:

[Returns] The decoded bitmap, or null if the image data could not be decoded, ...

它在出错时返回 null,而不是抛出异常,因此您需要检查返回值。

关于自定义类中的 Android SurfaceView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37150656/

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