gpt4 book ai didi

java - 安卓 IllegalArgumentException lockCanvas()

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:12:07 39 4
gpt4 key购买 nike

我一直在为这个异常而苦苦挣扎,我环顾四周,但没有任何帮助。

这是代码

package com.example.surfacetest;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.Surface.OutOfResourcesException;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class BoardSurfaceActivity extends Activity {
/** Called when the activity is first created. */

private static final String TAG = BoardSurfaceActivity.class.getSimpleName();
private BoardSurface bS;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bS = new BoardSurface(this);
setContentView(bS);
Log.d(TAG, "View added");
}

@Override
protected void onDestroy() {
Log.d(TAG, "Destroying...");
super.onDestroy();
}

@Override
protected void onStop() {
Log.d(TAG, "Stopping...");
super.onStop();
}

@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
}

@Override
protected void onPause() {
Log.d(TAG, "Pausing...");
super.onPause();
}

@Override
protected void onResume() {
Log.d(TAG, "Resuming...");
super.onResume();

}

public class BoardSurface extends SurfaceView implements SurfaceHolder.Callback, Runnable {
final String TAG = BoardSurface.class.getSimpleName();
private Stuff stuff;
Thread t = null;
SurfaceHolder holder;
boolean isItOk = false;

public BoardSurface(Context context) {
super(context);
holder = getHolder();
getHolder().addCallback(this);
stuff = new Stuff(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher), 50, 50);
t = new Thread(this);
setFocusable(true);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
Log.d(TAG, "Surface created");
resume();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d(TAG, "Surface Destroyed");
pause();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// delegating event handling to the droid
stuff.handleActionDown((int) event.getX(), (int) event.getY());

// check if in the lower part of the screen we exit
if (event.getY() > getHeight() - 50) {
((Activity) getContext()).finish();
} else {
Log.d(TAG, "Coords: x=" + event.getX() + ",y=" + event.getY());
}
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
// the gestures
if (stuff.isTouched()) {
// the droid was picked up and is being dragged
stuff.setX((int) event.getX());
stuff.setY((int) event.getY());
}
}
if (event.getAction() == MotionEvent.ACTION_UP) {
// touch was released
if (stuff.isTouched()) {
stuff.setTouched(false);
}
}
return true;
}

public void draw(Canvas canvas) {
canvas.drawColor(Color.BLUE);
stuff.draw(canvas);
}

@Override
public void run() {
while (isItOk) {
holder = getHolder();
if (holder.getSurface().isValid()) {

Canvas c = null;
try {
// make sure holder is updated
c = holder.lockCanvas(null);
if (c != null) {
synchronized (holder) {
draw(c);
}
}
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (c != null) {
holder.unlockCanvasAndPost(c);
}
}
}
}
}

public void pause() {
isItOk = false;
while (true) {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
t = null;
}

public void resume() {
if (t.getState() == Thread.State.TERMINATED) {
t = new Thread(this);
isItOk = true;
t.start();
} else {
isItOk = true;
t.start();
}

}
}
}

super 奇怪的是我有这个痕迹:

10-22 08:54:41.153: D/BoardSurface(17298): Surface created
10-22 08:54:41.394: E/memalloc(17298): /dev/pmem: Failed to map buffer size:24641536 offset:23699456 fd:56 Error: Invalid argument
10-22 08:54:41.394: E/gralloc(17298): Could not mmap handle 0x89e2b8, fd=56 (Invalid argument)
10-22 08:54:41.394: E/gralloc(17298): gralloc_register_buffer: gralloc_map failed
10-22 08:54:41.394: W/GraphicBufferMapper(17298): registerBuffer(0x89e2b8) failed -22 (Invalid argument)
10-22 08:54:41.394: E/GraphicBuffer(17298): unflatten: registerBuffer failed: Invalid argument (-22)
10-22 08:54:41.394: E/memalloc(17298): /dev/pmem: Failed to map buffer size:24641536 offset:23699456 fd:56 Error: Invalid argument
10-22 08:54:41.394: E/gralloc(17298): Could not mmap handle 0x89e2b8, fd=56 (Invalid argument)
10-22 08:54:41.394: E/libgenlock(17298): perform_lock_unlock_operation: GENLOCK_IOC_DREADLOCK failed (lockType0x1,err=Bad file number fd=56)
10-22 08:54:41.394: E/gralloc(17298): gralloc_lock: genlock_lock_buffer (lockType=0x2) failed
10-22 08:54:41.394: W/GraphicBufferMapper(17298): lock(...) failed -22 (Invalid argument)
10-22 08:54:41.394: W/Surface(17298): failed locking buffer (handle = 0x89e2b8)
10-22 08:54:41.424: E/SurfaceHolder(17298): Exception locking surface
10-22 08:54:41.424: E/SurfaceHolder(17298): java.lang.IllegalArgumentException
10-22 08:54:41.424: E/SurfaceHolder(17298): at android.view.Surface.nativeLockCanvas(Native Method)
10-22 08:54:41.424: E/SurfaceHolder(17298): at android.view.Surface.lockCanvas(Surface.java:236)
10-22 08:54:41.424: E/SurfaceHolder(17298): at android.view.SurfaceView$4.internalLockCanvas(SurfaceView.java:807)
10-22 08:54:41.424: E/SurfaceHolder(17298): at android.view.SurfaceView$4.lockCanvas(SurfaceView.java:787)
10-22 08:54:41.424: E/SurfaceHolder(17298): at com.example.surfacetest.BoardSurfaceActivity$BoardSurface.run(BoardSurfaceActivity.java:137)
10-22 08:54:41.424: E/SurfaceHolder(17298): at java.lang.Thread.run(Thread.java:841)

在我的运行循环中。

但这是棘手的部分!当我按返回(不是 HOME)时,然后再次从菜单启动应用程序。它只是简单地工作。

所以我真的不知道我错过了什么。

其他信息:我在 HTC Sensation XE、CM 10.2 (Android 4.3.1) 上运行

编辑:

设备不是这里的问题。尝试使用另一台 HTC 的库存 ROM。

编辑 2:

即使我检查:

if (holder.getSurface().isValid())

具体是说如果 lockcanvas() 为真就会成功,我得到这个异常

10-22 10:11:27.688: E/SurfaceHolder(22195): Exception locking surface
10-22 10:11:27.688: E/SurfaceHolder(22195): java.lang.IllegalArgumentException
10-22 10:11:27.688: E/SurfaceHolder(22195): at android.view.Surface.nativeLockCanvas(Native Method)
10-22 10:11:27.688: E/SurfaceHolder(22195): at android.view.Surface.lockCanvas(Surface.java:236)
10-22 10:11:27.688: E/SurfaceHolder(22195): at android.view.SurfaceView$4.internalLockCanvas(SurfaceView.java:807)
10-22 10:11:27.688: E/SurfaceHolder(22195): at android.view.SurfaceView$4.lockCanvas(SurfaceView.java:787)
10-22 10:11:27.688: E/SurfaceHolder(22195): at com.example.surfacetest.BoardSurfaceActivity$BoardSurface.run(BoardSurfaceActivity.java:144)
10-22 10:11:27.688: E/SurfaceHolder(22195): at java.lang.Thread.run(Thread.java:841)

编辑 3:

适用于 Galaxy S4 Stock ROM 和模拟器 Intelx86 4.2.2

最佳答案

@CinetiK:发布剩余的堆栈跟踪信息。

HTC 设备在旋转和表面测试方面臭名昭著。你试过这个吗:

  1. 重启手机。

  2. 进入安全模式(即重启手机,当您看到 HTcflags时,长按音量调低按钮)。手机将再次重启并进入安全模式。

  3. 手机进入安全模式后,转到设置 |显示与手势 |重力传感器校准。然后,校准您的手机,同时确保您的手机放在平坦的表面上。校准成功完成后,以正常模式重启手机。

如果上述步骤仍未修复错误,并且由于您使用的是 CM 10.2,我建议您先在库存 ROM 上测试您的 Android 应用。虽然我并不反对自定义 Android ROM(我自己使用过它们),但根据经验,我在自定义 ROM 上遇到的无法解释的错误比普通 ROM 多。先试试原版 ROM,看看你是否仍然遇到同样的错误。

关于java - 安卓 IllegalArgumentException lockCanvas(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19498113/

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