Пытаюсь написать своё первое приложение на Android Studio. И так сложилось что пришлось делать Судоку, для проекта. Но вот беда. Сделать, вроде сделал. Но приложения вылетает без каких либо ошибок. Не могли бы вы помочь мне в решении этой проблемы. Файл MainActivity:
package com.example.sudoku408;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private GridView mGridView;
private Button b1,b2,b3,b4,b5,b6,b7,b8,b9;
private String selectedButton = "n1";
private Game mGame;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGame = new Game(this);
getUIItems();
mGridView.setNumColumns(9);
mGridView.setEnabled(true);
mGridView.setAdapter(mGame);
mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mGame.setNumber(position, selectedButton);
}
});
}
private void getUIItems() {
mGridView = (GridView) findViewById(R.id.field);
b1 = (Button) findViewById(R.id.btn1);
b2 = (Button) findViewById(R.id.btn2);
b3 = (Button) findViewById(R.id.btn3);
b4 = (Button) findViewById(R.id.btn4);
b5 = (Button) findViewById(R.id.btn5);
b6 = (Button) findViewById(R.id.btn6);
b7 = (Button) findViewById(R.id.btn7);
b8 = (Button) findViewById(R.id.btn8);
b9 = (Button) findViewById(R.id.btn9);
}
public void setOnItemClickListener() {
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);
}
@Override
public void onClick(View view){
switch (view.getId()){
case R.id.btn1: selectedButton = "n1";
break;
case R.id.btn2: selectedButton = "n2";
break;
case R.id.btn3: selectedButton = "n3";
break;
case R.id.btn4: selectedButton = "n4";
break;
case R.id.btn5: selectedButton = "n5";
break;
case R.id.btn6: selectedButton = "n6";
break;
case R.id.btn7: selectedButton = "n7";
break;
case R.id.btn8: selectedButton = "n8";
break;
case R.id.btn9: selectedButton = "n9";
break;
}
}
}
Файл AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sudoku408">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Sudoku408">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Файл Game.java
package com.example.sudoku408;
import android.content.Context;
import android.content.res.Resources;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import java.util.ArrayList;
class Game extends BaseAdapter {
private Context mContext;
private final Integer mRows = 9, mCols = 9;
private int numberArray[][] = new int[mRows][mCols];
private Resources mRes;
private ArrayList<String> arrPict;
int UnblockPosition[] = new int[mRows*mCols];
int helperArray[][];
public Game(Context mContext){
this.mContext = mContext;
arrPict = new ArrayList<>(mCols*mRows);
mRes = mContext.getResources();
}
@Override
public int getCount() {
return mRows*mCols;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
ImageView imageView;
if (view == null){
imageView = new ImageView(mContext);
} else {
imageView = (ImageView) view;
}
Integer drawableId = mRes.getIdentifier(arrPict.get(position), "drawable", mContext.getPackageName());
imageView.setImageResource(drawableId);
return imageView;
}
private void createField(){
initArray();
for (int i = 0; i < mRows; i++){
for (int j = 0; j < mCols; j++) {
arrPict.add("n" + numberArray[i][j]);
}
}
helperArray = numberArray;
}
public int getRow(int position){
int row = 1;
if (position<=8){
return 0;
}else {
while(position>=0 && position <9) {
row++;
}
while(position>=9){
position = position - 9;
row++;
}
return row-1;
}
}
public int getCell(int position){
if (position<=8){
return position;
}else {
return position % 9;
}
}
private void initArray(){
for (int i = 0; i < mRows; i++){
for (int j = 0; j < mCols; j++) {
numberArray[i][j] = j+1;
}
}
}
public void setNumber(int position, String selectedButton){
for (int i = 0; i < UnblockPosition.length; i++){
if (UnblockPosition[i] == position){
arrPict.set(position, selectedButton);
helperArray[getRow(position)][getCell(position)] = Integer.parseInt(selectedButton.split("n")[1]);
notifyDataSetChanged();
}
}
}
}
Файл activity_main:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<GridView
android:id="@+id/field"
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="@drawable/sudoku"
android:onClick="onClick"></GridView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#c5718079"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/btn1"
android:layout_width="40dp"
android:layout_height="40dp"
android:text="1"
android:layout_marginRight="5dp"></Button>
<Button
android:id="@+id/btn2"
android:layout_width="40dp"
android:layout_height="40dp"
android:text="2"
android:layout_marginRight="5dp"></Button>
<Button
android:id="@+id/btn3"
android:layout_width="40dp"
android:layout_height="40dp"
android:text="3"
android:layout_marginRight="5dp"></Button>
<Button
android:id="@+id/btn4"
android:layout_width="40dp"
android:layout_height="40dp"
android:text="4"
android:layout_marginRight="5dp"></Button>
<Button
android:id="@+id/btn5"
android:layout_width="40dp"
android:layout_height="40dp"
android:text="5"
android:layout_marginRight="5dp"></Button>
<Button
android:id="@+id/btn6"
android:layout_width="40dp"
android:layout_height="40dp"
android:text="6"
android:layout_marginRight="5dp"></Button>
<Button
android:id="@+id/btn7"
android:layout_width="40dp"
android:layout_height="40dp"
android:text="7"
android:layout_marginRight="5dp"></Button>
<Button
android:id="@+id/btn8"
android:layout_width="40dp"
android:layout_height="40dp"
android:text="8"
android:layout_marginRight="5dp"></Button>
<Button
android:id="@+id/btn9"
android:layout_width="40dp"
android:layout_height="40dp"
android:text="9"></Button>
</LinearLayout>
</LinearLayout>
P.S android:background="@drawable/sudoku" - sudoku это просто картинка с клетками судоку n1, n2, n3, n4, n5, n6, n7, n8, n9 - это картинки с цифрами для заполнения судоку.