gpt4 book ai didi

java - 无法在未调用 Looper.prepare android 的线程内创建处理程序

转载 作者:行者123 更新时间:2023-12-01 13:58:45 24 4
gpt4 key购买 nike

我有一个问题,调用我在主题中编写的错误。我有 3 个类,其中一个是“主”类,而 Pavement 类则应根据 Level 类中的字符串在屏幕上添加路面对象。我该怎么做才能将该方法保留在 Pavement 类中并消除该错误。在这里我应用来源:开始:

package com.example.simplerpgdorid;

import java.util.ArrayList;
import java.util.Iterator;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;

public class Start extends Activity implements OnTouchListener{
Canvas c;
OurView ov;
Bitmap champ, grass, arrows;
static Bitmap pave;
Character character;
Pavement pavement;
float x, y;
char direction = 'x';
int topArrow[] = {370, 100, 430, 122};
int bottomArrow[] = {370, 195, 430, 223};
int leftArrow[] = {342, 130, 366, 184};
int rightArrow[] = {434, 130, 464, 184};
int lvlx;
public static int gowno = 0;

static Iterator<Locatable> itrLocatables;
static ArrayList<Pavement> pavements = new ArrayList<Pavement>();
static ArrayList<Locatable> locatables = new ArrayList<Locatable>();
static ArrayList<Locatable> locatablesArray = new ArrayList<Locatable>();

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Level lll = new Level();
ov = new OurView(this);
lvlx = 1;

champ = BitmapFactory.decodeResource(getResources(), R.drawable.s1);
grass = BitmapFactory.decodeResource(getResources(), R.drawable.grass);
arrows = BitmapFactory.decodeResource(getResources(), R.drawable.arrows);
pave = BitmapFactory.decodeResource(getResources(), R.drawable.pavement);



character = new Character(ov, champ);
pavement = new Pavement(3, 3, pave);
pavement.setBitmap(pave);

initLocatables();
initLocatables2();

ov.setOnTouchListener(this);
setContentView(ov);
}



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



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



@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

switch(event.getAction()){
case MotionEvent.ACTION_MOVE:
x = event.getX();
y = event.getY();

if(x > topArrow[0] && x < topArrow[2] && y > topArrow[1] && y < topArrow[3]){
direction = 'w';
}
else if(x > bottomArrow[0] && x < bottomArrow[2] && y > bottomArrow[1] && y < bottomArrow[3]){
direction = 's';
}
else if(x > leftArrow[0] && x < leftArrow[2] && y > leftArrow[1] && y < leftArrow[3]){
direction = 'a';
}
else if(x > rightArrow[0] && x < rightArrow[2] && y > rightArrow[1] && y < rightArrow[3]){
direction = 'd';
}else{
direction = 'x';
}
break;

case MotionEvent.ACTION_DOWN:

break;

case MotionEvent.ACTION_UP:

break;
}

return true;
}


public class OurView extends SurfaceView implements Runnable{

Thread thread = null;
boolean isGoing = false, spriteLoaded = false;
SurfaceHolder holder;

public OurView(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder = getHolder();
}

@Override
public void run() {
// TODO Auto-generated method stub
while(isGoing){
if(!holder.getSurface().isValid()){
continue;
}
for(Locatable l : locatables)
{
l.isDeInited();
l.isInited();
}
pavement.deInit(Level.lvl[lvlx]);
pavement.init(Level.lvl[lvlx]);
c = holder.lockCanvas();
draw(c);
holder.unlockCanvasAndPost(c);
}
}

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

public void resume(){
isGoing = true;
thread = new Thread(this);
thread.start();

}

public void draw(Canvas canvas){
canvas.drawARGB(255, 100, 100, 100);
canvas.drawBitmap(grass, 0, 0, null);


Paint paint = new Paint();
paint.setColor(Color.GRAY);
paint.setStyle(Style.FILL);
paint.setTextSize(15);
Rect UI = new Rect();
UI.set(320, 0, getWidth(), getHeight());
canvas.drawRect(UI, paint);
canvas.drawBitmap(arrows, getWidth() - arrows.getWidth() - 16, getHeight()/2 - arrows.getHeight()/2, null);




initLocatables();
initLocatables2();
for (@SuppressWarnings("rawtypes")Iterator i = locatablesArray.listIterator(); i.hasNext();)
{
Locatable l = (Locatable) i.next();
l.draw(canvas);
}
locatablesArray.clear();




pavement.draw(canvas);

//Debug
character.draw(canvas, direction);
paint.setColor(Color.RED);
canvas.drawText("x = " + x + " y = " + y + " " + gowno , 20, 20, paint);
canvas.drawCircle(x, y, 5, paint);
}
}

public void initLocatables()
{
locatables.add(pavement);
}
public void initLocatables2()
{
locatablesArray.addAll(pavements);
itrLocatables = locatablesArray.listIterator();
}

}

路面:

package com.example.simplerpgdorid;

import com.example.simplerpgdorid.Start.OurView;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.Log;

public class Pavement extends Activity implements Locatable{

OurView v;
Bitmap b = null;
int x, y, size;
boolean isInited = false, isDeInited;

public Pavement(int x, int y, Bitmap bit){
this.x = x * 32;
this.y = y * 32;
this.b = bit;
}



public void init(String[] xlevel){
Start.pavements.add(new Pavement(32, 32, getbitmap()));
Start.pavements.add(new Pavement(52, 52, getbitmap()));
Start.pavements.add(new Pavement(22 , 66, getbitmap()));
if(!isInited){
for(int z = 0; y < Level.lvl.length; y++){
for(int s = 0; x < Level.lvl.length; x++){
if(xlevel[z].charAt(s) == 'p'){
Start.pavements.add(new Pavement(z, s, getbitmap()));

}
Start.gowno ++;
}
}
}
isInited = true;
}

public void deInit(String[] xlevel)
{
if(!isDeInited)
for(int y = 0; y < Start.pavements.size(); y++)
{
Start.pavements.get(y).x = -100;
Start.pavements.get(y).y = -100;
}
isDeInited = true;
}

@Override
public void draw(Canvas canvas) {
// TODO Auto-generated method stub
canvas.drawBitmap(getbitmap(), x, y, null);
}
@Override
public void isInited() {
// TODO Auto-generated method stub
isInited = false;
}
@Override
public void isDeInited() {
// TODO Auto-generated method stub
isDeInited = false;
}

@Override
public Bitmap getbitmap() {
// TODO Auto-generated method stub
Bitmap bmp = null;
bmp = b;
return bmp;
}

@Override
public void setBitmap(Bitmap bmp) {
// TODO Auto-generated method stub
this.b = bmp;
}

}

和级别:

package com.example.simplerpgdorid;

public class Level {
public static String lvl[][] = new String[2][];
public Level(){
lvl[0] = new String[]{
"...p.....p",
"...p.....p",
"...p.....p",
"...p.....p",
"...p.....p",
"...p.....p",
"...p.....p",
"...p.....p",
"...p.....p",
"...p.....p",
};
lvl[1] = new String[]{
"...p.....p",
"...p.....p",
"...p.....p",
"...p.....p",
"...p.pp..p",
"...p.....p",
"...p.....p",
"...p.....p",
"...p.....p",
"...p.....p",
};
}
}

最佳答案

当我的 Android 库在某些 native 引擎(如 Unity)中使用时,我遇到了这些问题。

当我不确定代码何时在需要循环器的线程上执行时,我使用了以下方法:

    if (Looper.myLooper() == null) {
Looper.prepare();
}

关于java - 无法在未调用 Looper.prepare android 的线程内创建处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19475589/

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