【发布时间】:2012-12-20 14:28:26
【问题描述】:
我正在编写一个小安卓游戏。它应该显示我作为 ImageButtons 实现的 4 只动物的随机序列。用户必须记住这个序列并在之后重复它。
我现在的问题是图像按钮显示的正确时机。
我得到以下 NullPointerException 并且不知道为什么。也许有人可以帮忙!?
这是我的主要活动:
package lichtenberger.paul;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageButton;
import android.widget.TextView;
public class Game extends Activity {
int Reihenfolge[] = new int[40];
Random generator = new Random();
public final int CAT = 0;
public final int MAN = 1;
public final int BIRD = 2;
public final int SHEEP = 3;
public Handler handler;
public Thread AnimalThread;
{for(int i = 0; i<40; i++)Reihenfolge[i]=generator.nextInt(4);}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
final ImageButton cat = (ImageButton)findViewById(R.id.catButton);
final ImageButton sheep = (ImageButton)findViewById(R.id.sheepButton);
final ImageButton man = (ImageButton)findViewById(R.id.manButton);
final ImageButton bird = (ImageButton)findViewById(R.id.birdButton);
final TextView score = (TextView)findViewById(R.id.scoreNTV);
handler = new Handler(){
@Override
public void handleMessage(Message msg){
switch (msg.what) {
case 0:
cat.setVisibility(1);
man.setVisibility(0);
bird.setVisibility(0);
sheep.setVisibility(0);
break;
case 1:
man.setVisibility(1);
bird.setVisibility(0);
sheep.setVisibility(0);
cat.setVisibility(0);
break;
case 2:
bird.setVisibility(1);
sheep.setVisibility(0);
cat.setVisibility(0);
man.setVisibility(0);
break;
case 3:
sheep.setVisibility(1);
cat.setVisibility(0);
man.setVisibility(0);
bird.setVisibility(0);
break;
}
}
};
ShowSequence show = new ShowSequence();
Thread showSeq = new Thread(show);
showSeq.start();
};
}
我的线程类:
package lichtenberger.paul;
public class ShowSequence extends Game implements Runnable{
@Override
public void run() {
show();
}
private void show() {
for(int i = 0; i<40; i++){
switch (Reihenfolge[i]) {
case 0:
try {
// this pauses the Thread: Alternative to doing stuff...
Thread.sleep(2000);
handler.sendEmptyMessage(CAT);
} catch (InterruptedException e) {
e.printStackTrace();}
break;
case 1:
try {
// this pauses the Thread: Alternative to doing stuff...
Thread.sleep(2000);
handler.sendEmptyMessage(MAN);
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
case 2:
try {
// this pauses the Thread: Alternative to doing stuff...
Thread.sleep(2000);
handler.sendEmptyMessage(BIRD);
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
case 3:
try {
// this pauses the Thread: Alternative to doing stuff...
Thread.sleep(2000);
handler.sendEmptyMessage(SHEEP);
} catch (InterruptedException e) {
e.printStackTrace();
}
default:
break;
}
}
}}
还有我的 LogCat:
12-20 15:20:34.974: ERROR/AndroidRuntime(598): FATAL EXCEPTION: Thread-75
12-20 15:20:34.974: ERROR/AndroidRuntime(598): java.lang.NullPointerException
12-20 15:20:34.974: ERROR/AndroidRuntime(598): at lichtenberger.paul.ShowSequence.show(ShowSequence.java:50)
12-20 15:20:34.974: ERROR/AndroidRuntime(598): at lichtenberger.paul.ShowSequence.run(ShowSequence.java:10)
12-20 15:20:34.974: ERROR/AndroidRuntime(598): at java.lang.Thread.run(Thread.java:856)
编辑:
一个 Activity 中的全部代码不起作用:
package lichtenberger.paul;
import java.util.Random;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageButton;
import android.widget.TextView;
public class Game extends Activity {
int Reihenfolge[] = new int[40];
Random generator = new Random();
public ImageButton cat;
public ImageButton man;
public ImageButton bird;
public ImageButton sheep;
public TextView score;
private static Handler handler;
public final int CAT = 0;
public final int MAN = 1;
public final int BIRD = 2;
public final int SHEEP = 3;
public Runnable showAnimal;
public Thread AnimalThread;
{for(int i = 0; i<40; i++)Reihenfolge[i]=generator.nextInt(4);}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
setupUI();
showSequence();
initHandler();
}
public void showSequence() {
showAnimal = new showAnimal();
AnimalThread = new Thread(showAnimal);
AnimalThread.start();
}
public void setupUI() {
cat = (ImageButton)findViewById(R.id.catButton);
sheep = (ImageButton)findViewById(R.id.sheepButton);
man = (ImageButton)findViewById(R.id.manButton);
bird = (ImageButton)findViewById(R.id.birdButton);
score = (TextView)findViewById(R.id.scoreNTV);
}
private void initHandler() {
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case CAT:
cat.setVisibility(1);
man.setVisibility(0);
bird.setVisibility(0);
sheep.setVisibility(0);
break;
case MAN:
man.setVisibility(1);
bird.setVisibility(0);
sheep.setVisibility(0);
cat.setVisibility(0);
break;
case BIRD:
bird.setVisibility(1);
sheep.setVisibility(0);
cat.setVisibility(0);
man.setVisibility(0);
break;
case SHEEP:
sheep.setVisibility(1);
cat.setVisibility(0);
man.setVisibility(0);
bird.setVisibility(0);
break;
}
}
};
}
class showAnimal implements Runnable {
public void run() {
show();
}
private void show() {
for(int i = 0;i<40;i++){
switch (Reihenfolge[i]) {
case 0:
try {
// this pauses the Thread: Alternative to doing stuff...
Thread.sleep(2000);
handler.sendEmptyMessage(CAT);
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
case 1:
try {
// this pauses the Thread: Alternative to doing stuff...
Thread.sleep(2000);
handler.sendEmptyMessage(MAN);
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
case 2:
try {
// this pauses the Thread: Alternative to doing stuff...
Thread.sleep(2000);
handler.sendEmptyMessage(SHEEP);
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
case 3:
try {
// this pauses the Thread: Alternative to doing stuff...
Thread.sleep(2000);
handler.sendEmptyMessage(BIRD);
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
}
}
}
}
感谢您的帮助;)
【问题讨论】:
-
ShowSequence 类的第 50 行是什么?
-
错误总是出现在向处理程序发送消息的行上。在这种情况下,它是: handler.sendEmptyMessage(BIRD);
-
您的类层次结构有些奇怪。在
Game.onCreate中,你实例化了new ShowSequence,它扩展了Game,尽管创建了新的Activity(它永远不会得到它的onCreate,顺便说一句)。也许你应该把你的Runnable写成Game里面的私有类? -
对,如果您在此方法“public void handleMessage(Message msg) {...}”中打印 msg 变量会得到什么?
-
我已经尝试将所有内容放入游戏活动中,但对我来说没有效果。它一次显示所有 4 张图片,而不是暂停线程 2 秒钟然后显示新图片。我像以前一样添加了整个班级的代码。也许有人有解决这个问题的想法。
标签: android multithreading nullpointerexception