0

Пытаюсь написать своё первое приложение на 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">
&lt;application
    android:allowBackup=&quot;true&quot;
    android:icon=&quot;@mipmap/ic_launcher&quot;
    android:label=&quot;@string/app_name&quot;
    android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
    android:supportsRtl=&quot;true&quot;
    android:theme=&quot;@style/Theme.Sudoku408&quot;&gt;
    &lt;activity android:name=&quot;.MainActivity&quot;&gt;
        &lt;intent-filter&gt;
            &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;

            &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
        &lt;/intent-filter&gt;
    &lt;/activity&gt;
&lt;/application&gt;

</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&lt;String&gt; arrPict;
int UnblockPosition[] = new int[mRows*mCols];
int helperArray[][];

public Game(Context mContext){
    this.mContext = mContext;
    arrPict = new ArrayList&lt;&gt;(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), &quot;drawable&quot;, mContext.getPackageName());
    imageView.setImageResource(drawableId);

    return imageView;
}

private void createField(){
    initArray();

    for (int i = 0; i &lt; mRows; i++){
        for (int j = 0; j &lt; mCols; j++) {
            arrPict.add(&quot;n&quot; + numberArray[i][j]);
        }
    }
    helperArray = numberArray;
}
public int getRow(int position){
    int row = 1;
    if (position&lt;=8){
        return 0;
    }else {
        while(position&gt;=0 &amp;&amp; position &lt;9) {
            row++;
        }
        while(position&gt;=9){
            position = position - 9;
            row++;
        }
        return  row-1;
    }
}
public int getCell(int position){
    if (position&lt;=8){
        return position;
    }else {
        return position % 9;
    }
}
private void initArray(){
    for (int i = 0; i &lt; mRows; i++){
        for (int j = 0; j &lt; mCols; j++) {
            numberArray[i][j] = j+1;
        }
    }
}
public void setNumber(int position, String selectedButton){
    for (int i = 0; i &lt; UnblockPosition.length; i++){
        if (UnblockPosition[i] == position){
            arrPict.set(position, selectedButton);
            helperArray[getRow(position)][getCell(position)] = Integer.parseInt(selectedButton.split(&quot;n&quot;)[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">
&lt;GridView
    android:id=&quot;@+id/field&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;400dp&quot;
    android:background=&quot;@drawable/sudoku&quot;
    android:onClick=&quot;onClick&quot;&gt;&lt;/GridView&gt;

&lt;LinearLayout
    android:layout_width=&quot;fill_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_marginTop=&quot;20dp&quot;
    android:background=&quot;#c5718079&quot;
    android:gravity=&quot;center&quot;
    android:orientation=&quot;horizontal&quot;&gt;

    &lt;Button
        android:id=&quot;@+id/btn1&quot;
        android:layout_width=&quot;40dp&quot;
        android:layout_height=&quot;40dp&quot;
        android:text=&quot;1&quot;
        android:layout_marginRight=&quot;5dp&quot;&gt;&lt;/Button&gt;
    &lt;Button
        android:id=&quot;@+id/btn2&quot;
        android:layout_width=&quot;40dp&quot;
        android:layout_height=&quot;40dp&quot;
        android:text=&quot;2&quot;
        android:layout_marginRight=&quot;5dp&quot;&gt;&lt;/Button&gt;
    &lt;Button
        android:id=&quot;@+id/btn3&quot;
        android:layout_width=&quot;40dp&quot;
        android:layout_height=&quot;40dp&quot;
        android:text=&quot;3&quot;
        android:layout_marginRight=&quot;5dp&quot;&gt;&lt;/Button&gt;
    &lt;Button
        android:id=&quot;@+id/btn4&quot;
        android:layout_width=&quot;40dp&quot;
        android:layout_height=&quot;40dp&quot;
        android:text=&quot;4&quot;
        android:layout_marginRight=&quot;5dp&quot;&gt;&lt;/Button&gt;
    &lt;Button
        android:id=&quot;@+id/btn5&quot;
        android:layout_width=&quot;40dp&quot;
        android:layout_height=&quot;40dp&quot;
        android:text=&quot;5&quot;
        android:layout_marginRight=&quot;5dp&quot;&gt;&lt;/Button&gt;
    &lt;Button
        android:id=&quot;@+id/btn6&quot;
        android:layout_width=&quot;40dp&quot;
        android:layout_height=&quot;40dp&quot;
        android:text=&quot;6&quot;
        android:layout_marginRight=&quot;5dp&quot;&gt;&lt;/Button&gt;
    &lt;Button
        android:id=&quot;@+id/btn7&quot;
        android:layout_width=&quot;40dp&quot;
        android:layout_height=&quot;40dp&quot;
        android:text=&quot;7&quot;
        android:layout_marginRight=&quot;5dp&quot;&gt;&lt;/Button&gt;
    &lt;Button
        android:id=&quot;@+id/btn8&quot;
        android:layout_width=&quot;40dp&quot;
        android:layout_height=&quot;40dp&quot;
        android:text=&quot;8&quot;
        android:layout_marginRight=&quot;5dp&quot;&gt;&lt;/Button&gt;
    &lt;Button
        android:id=&quot;@+id/btn9&quot;
        android:layout_width=&quot;40dp&quot;
        android:layout_height=&quot;40dp&quot;
        android:text=&quot;9&quot;&gt;&lt;/Button&gt;
&lt;/LinearLayout&gt;

</LinearLayout>

P.S android:background="@drawable/sudoku" - sudoku это просто картинка с клетками судоку n1, n2, n3, n4, n5, n6, n7, n8, n9 - это картинки с цифрами для заполнения судоку.

0 Answers0