secondScreen.java
public class secondScreen extends Activity实现了View.OnClickListener {
//drawables
int res[] = new int[] {R.drawable.brownbars,R.drawable.centeredorangedot, R.drawable.dots, R.drawable.greenlines, R.drawable.lightbulb, R.drawable.orangedots, R.drawable.orangelines, R.drawable.tree, R.drawable.yellow, R.drawable.yellowwithred, R.drawable.brownbars,R.drawable.centeredorangedot,
R.drawable.dots, R.drawable.greenlines, R.drawable.lightbulb, R.drawable.orangedots, R.drawable.orangelines, R.drawable.tree, R.drawable.yellow, R.drawable.yellowwithred};
int lay1, lay2, shuffleCount = 0, gameCount = 0;
ImageButton first, second;
LinearLayout layout;
ImageButton b1;
ImageButton b2;
ImageButton b3;
ImageButton b4;
ImageButton b5;
ImageButton b6;
ImageButton b7;
ImageButton b8;
ImageButton b9;
ImageButton b10;
ImageButton b11;
ImageButton b12;
ImageButton b13;
ImageButton b14;
ImageButton b15;
ImageButton b16;
ImageButton b17;
ImageButton b18;
ImageButton b19;
ImageButton b20;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
Intent activityThatCalled = getIntent();
//initialize all buttons in game
b1 = (ImageButton) findViewById(R.id.b1);
b2 = (ImageButton) findViewById(R.id.b2);
b3 = (ImageButton) findViewById(R.id.b3);
b4 = (ImageButton) findViewById(R.id.b4);
b5 = (ImageButton) findViewById(R.id.b5);
b6 = (ImageButton) findViewById(R.id.b6);
b7 = (ImageButton) findViewById(R.id.b7);
b8 = (ImageButton) findViewById(R.id.b8);
b9 = (ImageButton) findViewById(R.id.b9);
b10 = (ImageButton) findViewById(R.id.b10);
b11 = (ImageButton) findViewById(R.id.b11);
b12 = (ImageButton) findViewById(R.id.b12);
b13 = (ImageButton) findViewById(R.id.b13);
b14 = (ImageButton) findViewById(R.id.b14);
b15 = (ImageButton) findViewById(R.id.b15);
b16 = (ImageButton) findViewById(R.id.b16);
b17 = (ImageButton) findViewById(R.id.b17);
b18 = (ImageButton) findViewById(R.id.b18);
b19 = (ImageButton) findViewById(R.id.b19);
b20 = (ImageButton) findViewById(R.id.b20);
iconRandomizer();
};
public void randomCheck(ImageButton btn, int image) {
if (gameCount < 2) {
gameCount++;
if (gameCount == 1) {
first = btn;
lay1 = image;
} else {
second = btn;
lay2 = image;
if (lay1 != lay2) {
ArrayList<View> touchables = layout.getTouchables();
for(View touchable : touchables) {
if (touchable instanceof Button)
((Button) touchable).setEnabled(false);
}
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
first.setImageResource(R.color.material_blue_grey_800);
second.setImageResource(R.color.material_blue_grey_800);
first.setEnabled(true);
second.setEnabled(true);
}
}, 1000);
}
gameCount = 0;
}
}
}
static void shuffleArray(int[] ar)
{
Random rnd = new Random();
for (int i = ar.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
// Simple swap
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
public void iconRandomizer() {
for (int i = 0; i < 10; i++)
System.out.println(res[i]);
shuffleArray(res);
for (int i = 0; i < 10; i++)
System.out.println(res[i]);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
b10.setOnClickListener(this);
b11.setOnClickListener(this);
b12.setOnClickListener(this);
b13.setOnClickListener(this);
b14.setOnClickListener(this);
b15.setOnClickListener(this);
b16.setOnClickListener(this);
b17.setOnClickListener(this);
b18.setOnClickListener(this);
b19.setOnClickListener(this);
b20.setOnClickListener(this);
}
public void onClick(View v){
switch (v.getId()){
case R.id.b1:
b1.setImageResource(res[0]);
b1.setEnabled(false);
randomCheck(b1, res[0]);
break;
case R.id.b2:
b2.setImageResource(res[1]);
b2.setEnabled(false);
randomCheck(b2,res[1]);
break;
case R.id.b3:
b3.setImageResource(res[2]);
b3.setEnabled(false);
randomCheck(b3, res[2]);
break;
case R.id.b4:
b4.setImageResource(res[3]);
b4.setEnabled(false);
randomCheck(b4, res[3]);
break;
case R.id.b5:
b5.setImageResource(res[4]);
b5.setEnabled(false);
randomCheck(b5, res[4]);
break;
case R.id.b6:
b6.setImageResource(res[5]);
b6.setEnabled(false);
randomCheck(b6, res[5]);
break;
case R.id.b7:
b7.setImageResource(res[6]);
b7.setEnabled(false);
randomCheck(b7, res[6]);
break;
case R.id.b8:
b8.setImageResource(res[7]);
b8.setEnabled(false);
randomCheck(b8, res[7]);
break;
case R.id.b9:
b9.setImageResource(res[8]);
b9.setEnabled(false);
randomCheck(b9, res[8]);
break;
case R.id.b10:
b10.setImageResource(res[9]);
b10.setEnabled(false);
randomCheck(b10, res[9]);
break;
case R.id.b11:
b11.setImageResource(res[10]);
b11.setEnabled(false);
randomCheck(b11, res[10]);
break;
case R.id.b12:
b12.setImageResource(res[11]);
b12.setEnabled(false);
randomCheck(b12, res[11]);
case R.id.b13:
b13.setImageResource(res[12]);
b13.setEnabled(false);
randomCheck(b13, res[12]);
break;
case R.id.b14:
b14.setImageResource(res[13]);
b14.setEnabled(false);
randomCheck(b14, res[13]);
case R.id.b15:
b15.setImageResource(res[14]);
b15.setEnabled(false);
randomCheck(b15, res[14]);
break;
case R.id.b16:
b16.setImageResource(res[15]);
b16.setEnabled(false);
randomCheck(b16, res[15]);
break;
case R.id.b17:
b17.setImageResource(res[16]);
b17.setEnabled(false);
randomCheck(b17, res[16]);
break;
case R.id.b18:
b18.setImageResource(res[17]);
b18.setEnabled(false);
randomCheck(b18, res[17]);
break;
case R.id.b19:
b19.setImageResource(res[18]);
b19.setEnabled(false);
randomCheck(b19, res[18]);
break;
case R.id.b20:
b20.setImageResource(res[19]);
b20.setEnabled(false);
randomCheck(b20, res[19]);
break;
}
}
//}
}
错误日志:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList android.widget.LinearLayout.getTouchables()' on a null object reference
at eagle.abhishekravi.abhishek.eagle.secondScreen.randomCheck(secondScreen.java:111)
at eagle.abhishekravi.abhishek.eagle.secondScreen.onClick(secondScreen.java:232)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
我正在制作记忆匹配游戏,我需要在点击第二个Button
后停用所有按钮。检查完成后,它将恢复正常。我已经让检查工作但仍然需要在这种情况下禁用按钮,用户在1秒暂停之间点击另一个Button
。让我知道您是否需要更多信息。谢谢!
答案 0 :(得分:0)
根据错误消息提供的信息,看起来layout
为空。如果您仍然遇到问题,那么设置layout
的代码将会非常有用。
您的布局应该通过R.layout.my_layout
访问。