我有一个应用程序,我完全通过编程来设置用户界面,这意味着我没有用XML绘制对象。它工作得非常好,但由于我用不同的类在内容中绘制对象,因此需要在两个类之间建立通信。例如,如果我在Class1中按下一个按钮,我想把文本写在Class2的按钮上,并以此触发Class2的按钮,我认为使用接口是最合理的解决方案,但我无法使用接口将数据从Class1传输到Class2。我收到一个错误消息,说“无法强制转换为...”
我的游戏界面:
public interface GameInterface {
void seciliHucreyiDoldur(String seciliHucreyiDoldur);
}
1类:
private class Cell implements GameInterface {
int value;
boolean fixed;
Button btn;
String hucreyiDolduracakVeri;
Context THIS;
public Cell(int initValue, Context THIS) {
this.THIS = THIS;
value = initValue;
if (value != 0) {
fixed = true;
} else {
fixed = false;
}
btn = new Button(THIS);
btn.setTextSize(16);
if (fixed) {
btn.setText(String.valueOf(value));
}
txtSayac.setGravity(Gravity.CENTER);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (fixed) {
Toast.makeText(THIS, "Bu hücreyi değiştiremezsiniz!", Toast.LENGTH_SHORT).show();
return;
}
/*value++;
if (value > 9) {
value = 1;
}*/
btn.setBackgroundResource(R.drawable.hucre_arkaplani_kirmizimsi);
btn.setTextColor(Color.BLACK);
btn.setText(hucreyiDolduracakVeri);
btn.setTextColor(Color.RED);
if (correct()) {
textView.setText("");
} else {
textView.setText("Tekrar eden rakamlar var, kontrol etmelisin!");
}
}
});
}
@Override
public void seciliHucreyiDoldur(String seciliHucreyiDoldur) {
this.hucreyiDolduracakVeri = seciliHucreyiDoldur;
Toast.makeText(THIS, hucreyiDolduracakVeri, Toast.LENGTH_SHORT).show();
if (fixed) {
//Toast.makeText(THIS, "Bu hücreyi değiştiremezsiniz!", Toast.LENGTH_SHORT).show();
return;
}
/*value++;
if (value > 9) {
value = 1;
}*/
btn.setTextColor(Color.BLACK);
btn.setText(seciliHucreyiDoldur);
btn.setTextColor(Color.RED);
btn.setBackgroundResource(R.drawable.hucre_arkaplani_mavi_acik);
if (correct()) {
textView.setText("");
} else {
textView.setText("Tekrar eden rakamlar var, kontrol etmelisin!");
}
}
@Override
public void hucreSeciliMi(boolean hucreSecilimi) {
}
}
第2类:
public class AltBolumNumericTablo implements GameInterface{
Button btnClick;
boolean onClicked;
int value;
Activity activity;
GameInterface gameInterface;
public AltBolumNumericTablo(int value, Context THIS, Activity activity) {
this.activity = activity;
this.value = value;
btnClick = new Button(THIS);
btnClick.setTextSize(24f);
gameInterface = (GameInterface) activity;
switch (value) {
case 0:
btnClick.setText("1");
break;
case 1:
btnClick.setText("2");
break;
case 2:
btnClick.setText("3");
break;
case 3:
btnClick.setText("4");
break;
case 4:
btnClick.setText("5");
break;
case 5:
btnClick.setText("6");
break;
case 6:
btnClick.setText("7");
break;
case 7:
btnClick.setText("8");
break;
case 8:
btnClick.setText("9");
break;
case 9:
btnClick.setBackgroundResource(R.drawable.sil_icon);
break;
}
btnClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickIslemleri(value);
}
});
}
public void clickIslemleri(int value) {
switch (value) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
gameInterface.seciliHucreyiDoldur(String.valueOf(value+1));
break;
case 9:
gameInterface.seciliHucreyiDoldur("");
break;
}
}
public void kayitliOyunuGuncelle() {
misafirOyunKaydiDB = new MisafirOyunKaydiDB(MainActivity.this);
new MisafirGirisliOyunKaydiDAO().misafirgirisiKaydiGuncelle(misafirOyunKaydiDB,
1, input + " s", 0, "00:45", kayitZamanini());
misafirKaydiListesi = new MisafirGirisliOyunKaydiDAO().tumOyunKayitlari(misafirOyunKaydiDB);
for (int i = 0; i < misafirKaydiListesi.size(); i++) {
Log.e("saved game", misafirKaydiListesi.get(i).getTimestamp());
}
}
public void gecmisOyunlarGuest() {
misafirOyunKaydiDB = new MisafirOyunKaydiDB(MainActivity.this);
misafirKaydiListesi = new MisafirGirisliOyunKaydiDAO().tumOyunKayitlari(misafirOyunKaydiDB);
for (int i = 0; i < misafirKaydiListesi.size(); i++) {
Log.e("saved game", misafirKaydiListesi.get(i).getId() + "");
}
}
@Override
public void seciliHucreyiDoldur(String seciliHucreyiDoldur) {
}
@Override
public void hucreSeciliMi(Boolean hucreSecilimi) {
}
}
错误:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.devapost.mysudoku, PID: 4945
java.lang.ClassCastException: com.devapost.mysudoku.MainActivity cannot be cast to com.devapost.mysudoku.utils.GameInterface
at com.devapost.mysudoku.MainActivity$AltBolumNumericTablo.<init>(MainActivity.java:514)
at com.devapost.mysudoku.MainActivity.altNumericDoldur(MainActivity.java:297)
MainActivity.java:
public class MainActivity extends AppCompatActivity implements GameInterface{
Cell[][] table;
AltBolumNumericTablo[][] tableAltNumeric;
MisafirGirisEkrani[] tableMisafir;
UyeMenuEkrani[] tableUyeMenusu;
String input;
TableLayout tableLayout;
TextView textView, txtSayac;
Button buttonGenerate;
LinearLayout linearLayout;
Button btnGoogleLoginMainScreen, btnMisafirMainScreen;
int hangiEkranda = 0;
MisafirOyunKaydiDB misafirOyunKaydiDB;
ArrayList<MisafirGirisliOyunKaydiModel> misafirKaydiListesi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
girisSecenekleriEkrani();
}
@Override
public void onBackPressed() {
switch (hangiEkranda) {
case 0:
case 2://üye menusundeyse
super.onBackPressed();
linearLayout.removeAllViews();
onBackPressed();
break;
case 1://misafir menusundeyse
girisSecenekleriEkrani();
break;
case 3://misafir oyun ekranındaysa
misafirOyunKaydiDB = new MisafirOyunKaydiDB(MainActivity.this);
new MisafirGirisliOyunKaydiDAO().saveGame(misafirOyunKaydiDB, input, input, 5, txtSayac.getText().toString(), 1, kayitZamanini());
misafirEkraniniDoldur();
break;
case 4://misafir ayarlardaysa
case 5://misafirden nasıl oynanıra gittiyse
misafirEkraniniDoldur();
break;
case 6://serüvene gittiyse
case 7://rastgele oyun kurma ekranına gittiyse
case 8://skorboard ekranına gittiyse
case 9://üye ekranından ayarlara gittiyse
case 10://üye ekranından nasıl oynanır ekranına gittiyse
uyelerinMenusu();
break;
}
}
public String kayitZamanini() {
Calendar anlikSaatDakika = Calendar.getInstance();
int yil = anlikSaatDakika.get(Calendar.HOUR_OF_DAY);
int ay = anlikSaatDakika.get(Calendar.MINUTE);
int gun = anlikSaatDakika.get(Calendar.SECOND);
int saat = anlikSaatDakika.get(Calendar.HOUR_OF_DAY);
int dakika = anlikSaatDakika.get(Calendar.MINUTE);
int saniye = anlikSaatDakika.get(Calendar.SECOND);
return gun + "." + ay + "." + yil + " - " + saat + ":" + dakika + ":" + saniye;
}
public void girisSecenekleriEkrani() {//giriş seçenekleri ekranı
hangiEkranda = 0;
linearLayout = new LinearLayout(MainActivity.this);
btnGoogleLoginMainScreen = new Button(MainActivity.this);
btnMisafirMainScreen = new Button(MainActivity.this);
btnGoogleLoginMainScreen.setText("Google İle Giriş Yap");
btnMisafirMainScreen.setText("Misafir Girişi");
btnGoogleLoginMainScreen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uyelerinMenusu();
}
});
btnMisafirMainScreen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearLayout.removeAllViews();
misafirEkraniniDoldur();
}
});
linearLayout.addView(btnGoogleLoginMainScreen);
linearLayout.addView(btnMisafirMainScreen);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT
);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setGravity(Gravity.CENTER);
linearLayout.setLayoutParams(layoutParams);
setContentView(linearLayout);
}
public void yeniOyunMisafirIcin() {
hangiEkranda = 3;
linearLayout.removeAllViews();
int N = 9, K = 25;
Sudoku sudoku = new Sudoku(N, K);
sudoku.fillValues();
txtSayac = new TextView(MainActivity.this);
input = sudoku.printSudoku();
String[] split = input.split(" ");
table = new Cell[9][9];
tableLayout = new TableLayout(MainActivity.this);
for (int i = 0; i < 9; i++) {
TableRow tableRow = new TableRow(MainActivity.this);
for (int j = 0; j < 9; j++) {
String s = split[i * 9 + j];
char c = s.charAt(0);
table[i][j] = new Cell(c == '?' ? 0 : c - '0', MainActivity.this);
paintCellBackground(i, j, table[i][j].btn);
tableRow.addView(table[i][j].btn);
}
tableLayout.addView(tableRow);
}
tableLayout.setShrinkAllColumns(true);
tableLayout.setStretchAllColumns(true);
textView = new TextView(MainActivity.this);
buttonGenerate = new Button(MainActivity.this);
linearLayout = new LinearLayout(MainActivity.this);
linearLayout.addView(txtSayac);
LinearLayout linearLayoutEkMetin = new LinearLayout(MainActivity.this);
linearLayoutEkMetin.setLayoutParams(
new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
linearLayoutEkMetin.setOrientation(LinearLayout.VERTICAL);
linearLayoutEkMetin.setGravity(Gravity.CENTER_HORIZONTAL);
buttonGenerate.setText("Generate");
buttonGenerate.setVisibility(View.GONE);
buttonGenerate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
misafirEkraniniDoldur();
progressDialog.dismiss();
}
});
linearLayoutEkMetin.addView(buttonGenerate);
linearLayout.addView(tableLayout);
linearLayout.addView(textView);
linearLayout.addView(linearLayoutEkMetin);
linearLayout.setOrientation(LinearLayout.VERTICAL);
altNumericDoldur();
setContentView(linearLayout);
}
public void paintCellBackground(int i, int j, Button button) {
switch (i) {
case 0:
case 1:
case 2:
case 6:
case 7:
case 8:
switch (j) {
case 0:
case 1:
case 2:
case 6:
case 7:
case 8:
button.setBackgroundResource(R.drawable.hucre_arkaplani_gri);
break;
case 3:
case 4:
case 5:
button.setBackgroundResource(R.drawable.hucre_arkaplani_beyaz);
break;
}
break;
case 3:
case 4:
case 5:
switch (j) {
case 0:
case 1:
case 2:
case 6:
case 7:
case 8:
button.setBackgroundResource(R.drawable.hucre_arkaplani_beyaz);
break;
case 3:
case 4:
case 5:
button.setBackgroundResource(R.drawable.hucre_arkaplani_gri);
break;
}
break;
}
}
public void misafirEkraniniDoldur() {//misafir ekranını doldurur
hangiEkranda = 1;
tableMisafir = new MisafirGirisEkrani[5];
tableLayout = new TableLayout(MainActivity.this);
for (int i = 0; i < 5; i++) {
TableRow tableRow = new TableRow(MainActivity.this);
tableMisafir[i] = new MisafirGirisEkrani(i, MainActivity.this);
tableRow.addView(tableMisafir[i].btnClick);
tableLayout.addView(tableRow);
}
tableLayout.setShrinkAllColumns(true);
tableLayout.setStretchAllColumns(true);
linearLayout = new LinearLayout(MainActivity.this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT
);
layoutParams.setMarginStart(16);
layoutParams.setMarginEnd(16);
tableLayout.setLayoutParams(layoutParams);
linearLayout.setGravity(Gravity.CENTER);
linearLayout.addView(tableLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);
setContentView(linearLayout);
}
public void altNumericDoldur() {//alt numerik tabloyu doldurur
tableAltNumeric = new AltBolumNumericTablo[2][5];
tableLayout = new TableLayout(MainActivity.this);
int butonID = 0;
for (int i = 0; i < 2; i++) {
TableRow tableRow = new TableRow(MainActivity.this);
for (int j = 0; j < 5; j++) {
tableAltNumeric[i][j] = new AltBolumNumericTablo(butonID, MainActivity.this,MainActivity.this);
tableRow.addView(tableAltNumeric[i][j].btnClick);
butonID++;
}
tableLayout.addView(tableRow);
}
tableLayout.setShrinkAllColumns(true);
tableLayout.setStretchAllColumns(true);
//linearLayout = new LinearLayout(MainActivity.this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT
);
layoutParams.setMarginStart(16);
layoutParams.setMarginEnd(16);
tableLayout.setLayoutParams(layoutParams);
linearLayout.addView(tableLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);
setContentView(linearLayout);
}
private void uyelerinMenusu() {//google ile giriş yapanlar için serüven ekranı
hangiEkranda = 2;
tableUyeMenusu = new UyeMenuEkrani[5];
tableLayout = new TableLayout(MainActivity.this);
for (int i = 0; i < 5; i++) {
TableRow tableRow = new TableRow(MainActivity.this);
tableUyeMenusu[i] = new UyeMenuEkrani(i, MainActivity.this);
tableRow.addView(tableUyeMenusu[i].btnClick);
tableLayout.addView(tableRow);
}
tableLayout.setShrinkAllColumns(true);
tableLayout.setStretchAllColumns(true);
linearLayout = new LinearLayout(MainActivity.this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT
);
layoutParams.setMarginStart(16);
layoutParams.setMarginEnd(16);
tableLayout.setLayoutParams(layoutParams);
linearLayout.setGravity(Gravity.CENTER);
linearLayout.addView(tableLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);
setContentView(linearLayout);
}
@Override
public void seciliHucreyiDoldur(String seciliHucreyiDoldur) {
}
@Override
public void hucreSeciliMi(boolean hucreSecilimi) {
}
public class UyeMenuEkrani {
Button btnClick;
boolean onClicked;
int value;
public UyeMenuEkrani(int value, Context THIS) {
this.value = value;
btnClick = new Button(THIS);
switch (value) {
case 0:
btnClick.setText("Serüven");
break;
case 1:
btnClick.setText("Rastgele Oyna");
break;
case 2:
btnClick.setText("Skor Tablosu");
break;
case 3:
btnClick.setText("Ayarlar");
break;
case 4:
btnClick.setText("Nasıl Oynanır");
break;
}
btnClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickIslemleri(value);
}
});
}
public void clickIslemleri(int value) {
switch (value) {
case 0://seruven
Toast.makeText(MainActivity.this, "serüvene gider", Toast.LENGTH_SHORT).show();
break;
case 1://rastgele
yeniOyunMisafirIcin();//rastgele ekranı misafir giriş ekranına gider, çok benzer zaten
break;
case 2://skorboard
Toast.makeText(MainActivity.this, "skorboard", Toast.LENGTH_SHORT).show();
break;
case 3://ayarlar
Toast.makeText(MainActivity.this, "ayarlar", Toast.LENGTH_SHORT).show();
break;
case 4://nasıl oynanır
Toast.makeText(MainActivity.this, "nasıl oynanır", Toast.LENGTH_SHORT).show();
break;
case 5:
break;
}
}
}
public class MisafirGirisEkrani {
Button btnClick;
boolean onClicked;
int value;
public MisafirGirisEkrani(int value, Context THIS) {
this.value = value;
btnClick = new Button(THIS);
switch (value) {
case 0:
btnClick.setText("Yeni Oyun");
break;
case 1:
btnClick.setText("Devam Et");
break;
case 2:
btnClick.setText("Ayarlar");
break;
case 3:
btnClick.setText("Giriş Yap");
break;
case 4:
btnClick.setText("Nasıl Oynanır");
break;
}
btnClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clickIslemleri(value);
}
});
}
public void clickIslemleri(int value) {
switch (value) {
case 0:
yeniOyunMisafirIcin();
break;
case 1://eski oyunları listele ve onlardan seçip devam et
gecmisOyunlarGuest();
break;
case 2:
kayitliOyunuGuncelle();
Toast.makeText(MainActivity.this, "ayarlar", Toast.LENGTH_SHORT).show();
break;
case 3:
girisSecenekleriEkrani();
break;
case 4:
Toast.makeText(MainActivity.this, "nasıl oynanır", Toast.LENGTH_SHORT).show();
break;
}
}
public void kayitliOyunuGuncelle() {
misafirOyunKaydiDB = new MisafirOyunKaydiDB(MainActivity.this);
new MisafirGirisliOyunKaydiDAO().misafirgirisiKaydiGuncelle(misafirOyunKaydiDB,
1, input + " s", 0, "00:45", kayitZamanini());
misafirKaydiListesi = new MisafirGirisliOyunKaydiDAO().tumOyunKayitlari(misafirOyunKaydiDB);
for (int i = 0; i < misafirKaydiListesi.size(); i++) {
Log.e("saved game", misafirKaydiListesi.get(i).getTimestamp());
}
}
public void gecmisOyunlarGuest() {
misafirOyunKaydiDB = new MisafirOyunKaydiDB(MainActivity.this);
misafirKaydiListesi = new MisafirGirisliOyunKaydiDAO().tumOyunKayitlari(misafirOyunKaydiDB);
for (int i = 0; i < misafirKaydiListesi.size(); i++) {
Log.e("saved game", misafirKaydiListesi.get(i).getId() + "");
}
}
}
boolean complated() {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (table[i][j].value == 0) {
return false;
}
}
}
return true;
}
boolean correct(int i1, int j1, int i2, int j2) {
boolean[] seen = new boolean[10];
for (int i = 1; i <= 9; i++) {
seen[i] = false;
}
for (int i = i1; i < i2; i++) {
for (int j = j1; j < j2; j++) {
int value = table[i][j].value;
if (value != 0) {
if (seen[value]) {
return false;
}
seen[value] = true;
}
}
}
return true;
}
boolean correct() {
for (int i = 0; i < 9; i++) {
if (!correct(i, 0, i + 1, 9)) {
return false;
}
}
for (int j = 0; j < 9; j++) {
if (!correct(0, j, 9, j + 1)) {
return false;
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (!correct(3 * i, 3 * j, 3 * i + 3, 3 * j + 3)) {
return false;
}
}
}
return true;
}
}
1条答案
按热度按时间kkih6yb81#
找到问题。问题位于
AltBolumNumericTablo
类中的以下提到的行您只需要按下面所述进行更改
从
至