我在寫一個小小的Android遊戲。它應該顯示一個隨機的4個動物序列,我將其實現爲ImageButtons。用戶必須記住這個序列並在之後重複。從線程得到NullPointerException
我現在的問題是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)
編輯:
整個代碼在一個活動沒有工作:
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行是什麼? – ademar111190
該錯誤總是發送消息給處理程序的線路上。在這種情況下,它是:handler.sendEmptyMessage(BIRD); –
你的類層次結構有點奇怪。在''Game.onCreate''中,你實例化''新的ShowSequence'',它擴展了''Game'',並創建了新的''Activity''(它永遠不會獲得它的''onCreate''稱爲btw)。也許你應該把''Runnable''寫成''Game''內的私人類而不是? – harism