私は小さなAndroidゲームを書いています。ImageButtons として実装した 4 匹の動物のランダムなシーケンスを表示する必要があります。ユーザーはこのシーケンスを覚えて、後で繰り返す必要があります。
私の問題は、Imagebuttons が表示される適切なタイミングです。
次の 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)
編集:
機能しなかった1つのアクティビティのコード全体:
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;
}
}
}
}
}
手伝ってくれてありがとう ;)