这个软件是闪退王,
Anonymous new View.OnClickListener() can be replaced with lambda
package com.itheima.sixyao;
import static com.itheima.sixyao.R.drawable.yy1;
import static com.itheima.sixyao.R.drawable.yy2;
import static com.itheima.sixyao.R.drawable.yy4;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
static int ss;
static int bb=0;
Button b1,b2;
ImageView i1 ,i2,i3,i4,i5,i6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
i1.findViewById(R.id.i1);
i2.findViewById(R.id.i2);
i3.findViewById(R.id.i3);
i4.findViewById(R.id.i4);
i5.findViewById(R.id.i5);
i6.findViewById(R.id.i6);
b1.findViewById(R.id.b1);
b2.findViewById(R.id.b2);
b1.setOnClickListener (new View.OnClickListener() {
@Override
public void onClick(View view) {
runOnUiThread(new Runnable() {
@Override
public void run() {
bb = bb + 1;
b1.setBackgroundColor(getResources().getColor(R.color.purple_200));
Random f = new Random();
ss = f.nextInt(4);
if (bb % 6 == 1) {
if (ss == 0) {
i1.setBackground(getResources().getDrawable(yy1));
}
if (ss == 1) {
i1.setBackground(getResources().getDrawable(yy2));
}
if (ss == 2) {
i1.setBackground(getResources().getDrawable(R.drawable.yy3));
}
if (ss == 3) {
i1.setBackground(getResources().getDrawable(yy4));
}
}
if (bb % 6 == 2) {
if (ss == 0) {
i2.setBackground(getResources().getDrawable(yy1));
}
if (ss == 1) {
i2.setBackground(getResources().getDrawable(yy2));
}
if (ss == 2) {
i2.setBackground(getResources().getDrawable(R.drawable.yy3));
}
if (ss == 3) {
i2.setBackground(getResources().getDrawable(yy4));
}
}
if (bb % 6 == 3) {
if (ss == 0) {
i3.setBackground(getResources().getDrawable(yy1));
}
if (ss == 1) {
i3.setBackground(getResources().getDrawable(yy2));
}
if (ss == 2) {
i3.setBackground(getResources().getDrawable(R.drawable.yy3));
}
if (ss == 3) {
i3.setBackground(getResources().getDrawable(yy4));
}
}
if (bb % 6 == 4) {
if (ss == 0) {
i4.setBackground(getResources().getDrawable(yy1));
}
if (ss == 1) {
i4.setBackground(getResources().getDrawable(yy2));
}
if (ss == 2) {
i4.setBackground(getResources().getDrawable(R.drawable.yy3));
}
if (ss == 3) {
i4.setBackground(getResources().getDrawable(yy4));
}
}
if (bb % 6 == 5) {
if (ss == 0) {
i5.setBackground(getResources().getDrawable(yy1));
}
if (ss == 1) {
i5.setBackground(getResources().getDrawable(yy2));
}
if (ss == 2) {
i5.setBackground(getResources().getDrawable(R.drawable.yy3));
}
if (ss == 3) {
i5.setBackground(getResources().getDrawable(R.drawable.yy4));
}
}
if (bb % 6 == 0) {
if (ss == 0) {
i6.setBackground(getResources().getDrawable(yy1));
}
if (ss == 1) {
i6.setBackground(getResources().getDrawable(yy2));
}
if (ss == 2) {
i6.setBackground(getResources().getDrawable(R.drawable.yy3));
}
if (ss == 3) {
i6.setBackground(getResources().getDrawable(yy4));
}
}
}
});
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
b2.setBackgroundColor(getResources().getColor(R.color.purple_200));
}
});
}
}
闪退
我好像觉得这是因为UI不能被主线程操作
能实现六爻占卜的功能
代码有点小问题,也提示你了Anonymous new View.OnClickListener() can be replaced with lambda,意思就是让你将 b1.setOnClickListener (new View.OnClickListener() {xxxx}换成lambda表达式来写
b1.setOnclicklistener(view
->
xxxx);
这是jdk1.8出现的新特性,匿名内部类用lambda表达式来写