不幸的是MyApp已经停止工作,我的代码中没有明显的错误

时间:2017-08-06 11:10:19

标签: java android xml

这是我的MainActivity.java代码:     包abhishekgidde.games.tictactoe;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    ImageView r1c1=(ImageView)findViewById(R.id.r1c1);
    ImageView r1c2=(ImageView)findViewById(R.id.r1c2);
    ImageView r1c3=(ImageView)findViewById(R.id.r1c3);
    ImageView r2c1=(ImageView)findViewById(R.id.r2c1);
    ImageView r2c2=(ImageView)findViewById(R.id.r2c2);
    ImageView r2c3=(ImageView)findViewById(R.id.r2c3);
    ImageView r3c1=(ImageView)findViewById(R.id.r3c1);
    ImageView r3c2=(ImageView)findViewById(R.id.r3c2);
    ImageView r3c3=(ImageView)findViewById(R.id.r3c3);
    ImageView mr1c1=(ImageView)findViewById(R.id.mr1c1);
    ImageView mr1c2=(ImageView)findViewById(R.id.mr1c2);
    ImageView mr1c3=(ImageView)findViewById(R.id.mr1c3);
    ImageView mr2c1=(ImageView)findViewById(R.id.mr2c1);
    ImageView mr2c2=(ImageView)findViewById(R.id.mr2c2);
    ImageView mr2c3=(ImageView)findViewById(R.id.mr2c3);
    ImageView mr3c1=(ImageView)findViewById(R.id.mr3c1);
    ImageView mr3c2=(ImageView)findViewById(R.id.mr3c2);
    ImageView mr3c3=(ImageView)findViewById(R.id.mr3c3);
    int turn=1;


public void play(View v)
{
String who_called_me = v.getTag().toString();
    if(turn%2==0)
    {
        //Player1 as even turns belong to p1
        //who_called_me the button at r1c1 will call function with int 11 so player1's bottle is visible
        switch(who_called_me)
        {
            case "11": r1c1.setAlpha(1f);
            case "12": r1c2.setAlpha(1f);
            case "13": r1c3.setAlpha(1f);
            case "21": r2c1.setAlpha(1f);
            case "22": r2c2.setAlpha(1f);
            case "23": r2c3.setAlpha(1f);
            case "31": r3c1.setAlpha(1f);
            case "32": r3c2.setAlpha(1f);
            case "33": r3c3.setAlpha(1f);
        }
turn++;
    }
    else if(turn%2==1)
    {
        //Player2 as odd turns belong to p2
        //who_called_me the button at r1c1 will call function with int 11 so player1's bottle is visible
        switch(who_called_me)
        {
            case "11": mr1c1.setAlpha(1f);
            case "12": mr1c2.setAlpha(1f);
            case "13": mr1c3.setAlpha(1f);
            case "21": mr2c1.setAlpha(1f);
            case "22": mr2c2.setAlpha(1f);
            case "23": mr2c3.setAlpha(1f);
            case "31": mr3c1.setAlpha(1f);
            case "32": mr3c2.setAlpha(1f);
            case "33": mr3c3.setAlpha(1f);
        }
        turn++;
    }
}
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //here goes my eraser
        r1c1.setAlpha(0f);
        r1c2.setAlpha(0f);
        r1c3.setAlpha(0f);
        r2c1.setAlpha(0f);
        r2c2.setAlpha(0f);
        r2c3.setAlpha(0f);
        r3c1.setAlpha(0f);
        r3c2.setAlpha(0f);
        r3c3.setAlpha(0f);
        mr1c1.setAlpha(0f);
        mr1c2.setAlpha(0f);
        mr1c3.setAlpha(0f);
        mr2c1.setAlpha(0f);
        mr2c2.setAlpha(0f);
        mr2c3.setAlpha(0f);
        mr3c1.setAlpha(0f);
        mr3c2.setAlpha(0f);
        mr3c3.setAlpha(0f);
    }

   /* public void clear_all() {
        r1c1.setAlpha(0f);
        r1c2.setAlpha(0f);
        r1c3.setAlpha(0f);
        r2c1.setAlpha(0f);
        r2c2.setAlpha(0f);
        r2c3.setAlpha(0f);
        r3c1.setAlpha(0f);
        r3c2.setAlpha(0f);
        r3c3.setAlpha(0f);
        mr1c1.setAlpha(0f);
        mr1c2.setAlpha(0f);
        mr1c3.setAlpha(0f);
        mr2c1.setAlpha(0f);
        mr2c2.setAlpha(0f);
        mr2c3.setAlpha(0f);
        mr3c1.setAlpha(0f);
        mr3c2.setAlpha(0f);
        mr3c3.setAlpha(0f);
    }*/
}

我的xml设计是:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="abhishekgidde.games.tictactoe.MainActivity">

    <ImageView
        android:id="@+id/r3c3"
        android:layout_width="105dp"
        android:layout_height="120dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.861"
        app:srcCompat="@drawable/harry" />

    <ImageView
        android:id="@+id/r3c2"
        android:layout_width="105dp"
        android:layout_height="120dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.861"
        app:srcCompat="@drawable/harry" />

    <ImageView
        android:id="@+id/r3c1"
        android:layout_width="105dp"
        android:layout_height="120dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.882"
        app:srcCompat="@drawable/harry" />

    <ImageView
        android:id="@+id/r2c1"
        android:layout_width="105dp"
        android:layout_height="120dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.498"
        app:srcCompat="@drawable/harry" />

    <ImageView
        android:id="@+id/r1c1"
        android:layout_width="105dp"
        android:layout_height="120dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.115"
        app:srcCompat="@drawable/harry" />

    <ImageView
        android:id="@+id/r2c2"
        android:layout_width="105dp"
        android:layout_height="120dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.498"
        app:srcCompat="@drawable/harry" />

    <ImageView
        android:id="@+id/r2c3"
        android:layout_width="105dp"
        android:layout_height="120dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.498"
        app:srcCompat="@drawable/harry" />

    <ImageView
        android:id="@+id/r1c3"
        android:layout_width="105dp"
        android:layout_height="120dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.138"
        app:srcCompat="@drawable/harry" />

    <ImageView
        android:id="@+id/r1c2"
        android:layout_width="105dp"
        android:layout_height="120dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.138"
        app:srcCompat="@drawable/harry" />

    <ImageView
        android:id="@+id/board"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="2dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.494"
        app:srcCompat="@drawable/board" />

    <ImageView
        android:id="@+id/mr3c3"
        android:layout_width="73dp"
        android:layout_height="110dp"
        android:layout_marginLeft="295dp"
        android:layout_marginTop="337dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/milk" />

    <ImageView
        android:id="@+id/mr3c2"
        android:layout_width="73dp"
        android:layout_height="110dp"
        android:layout_marginLeft="156dp"
        android:layout_marginTop="347dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/milk" />

    <ImageView
        android:id="@+id/mr3c1"
        android:layout_width="73dp"
        android:layout_height="110dp"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="337dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/milk" />

    <ImageView
        android:id="@+id/mr2c1"
        android:layout_width="73dp"
        android:layout_height="110dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="200dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/milk" />

    <ImageView
        android:id="@+id/mr2c2"
        android:layout_width="73dp"
        android:layout_height="110dp"
        android:layout_marginLeft="156dp"
        android:layout_marginTop="195dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/milk" />

    <ImageView
        android:id="@+id/mr2c3"
        android:layout_width="73dp"
        android:layout_height="110dp"
        android:layout_marginLeft="295dp"
        android:layout_marginTop="200dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/milk" />

    <ImageView
        android:id="@+id/mr1c3"
        android:layout_width="73dp"
        android:layout_height="110dp"
        android:layout_marginLeft="295dp"
        android:layout_marginTop="55dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/milk" />

    <ImageView
        android:id="@+id/mr1c2"
        android:layout_width="73dp"
        android:layout_height="110dp"
        android:layout_marginLeft="156dp"
        android:layout_marginTop="64dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/milk" />

    <ImageView
        android:id="@+id/mr1c1"
        android:layout_width="73dp"
        android:layout_height="110dp"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="64dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/milk" />

    <Button
        android:id="@+id/button"
        android:layout_width="67dp"
        android:layout_height="85dp"
        android:layout_marginLeft="301dp"
        android:text="Claim"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintVertical_bias="0.849"
        android:tag="33"/>

    <Button
        android:id="@+id/button9"
        android:layout_width="67dp"
        android:layout_height="85dp"
        android:layout_marginLeft="159dp"
        android:text="Claim"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.849"
        android:tag="32"/>

    <Button
        android:id="@+id/button8"
        android:layout_width="67dp"
        android:layout_height="85dp"
        android:layout_marginLeft="22dp"
        android:text="Claim"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.849"
        android:tag="31"/>

    <Button
        android:id="@+id/button7"
        android:layout_width="67dp"
        android:layout_height="85dp"
        android:layout_marginLeft="16dp"
        android:text="Claim"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:tag="21"/>

    <Button
        android:id="@+id/button6"
        android:layout_width="67dp"
        android:layout_height="85dp"
        android:layout_marginLeft="159dp"
        android:text="Claim"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:tag="22"/>

    <Button
        android:id="@+id/button5"
        android:layout_width="67dp"
        android:layout_height="85dp"
        android:layout_marginLeft="295dp"
        android:text="Claim"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:tag="23"/>

    <Button
        android:id="@+id/button4"
        android:layout_width="67dp"
        android:layout_height="85dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="295dp"
        android:layout_marginTop="8dp"
        android:onClick="play"
        android:tag="13"
        android:text="Claim"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.136" />

    <Button
        android:id="@+id/button3"
        android:layout_width="67dp"
        android:layout_height="85dp"
        android:layout_marginLeft="159dp"
        android:layout_marginTop="64dp"
        android:onClick="play"
        android:tag="12"
        android:text="Claim"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="67dp"
        android:layout_height="85dp"
        android:layout_marginLeft="25dp"
        android:onClick="play"
        android:text="Claim"
        app:layout_constraintLeft_toLeftOf="parent"
        android:tag="11"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginTop="80dp" />

</android.support.constraint.ConstraintLayout>

每当我构建apk时,apk都会成功生成,当我运行应用程序时,它会崩溃。 在这里,我想做的是做一个井字游戏 我试图实现的逻辑是一个主要的游戏板图像它是基础,在它上面每个框中有两个图像(第一个框r1c1称为第1行第1列)

3 个答案:

答案 0 :(得分:1)

在致电setContentView之前,您应致电findViewById。在您的活动中实施onCreate方法并首先设置内容视图。

setContentView(R.layout.your_layout_name); r1c1 = (ImageView) findViewById(R.id.r1c1); ...

答案 1 :(得分:1)

更改你的onCreate方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


ImageView r1c1=(ImageView)findViewById(R.id.r1c1);
ImageView r1c2=(ImageView)findViewById(R.id.r1c2);
ImageView r1c3=(ImageView)findViewById(R.id.r1c3);
ImageView r2c1=(ImageView)findViewById(R.id.r2c1);
ImageView r2c2=(ImageView)findViewById(R.id.r2c2);
ImageView r2c3=(ImageView)findViewById(R.id.r2c3);
ImageView r3c1=(ImageView)findViewById(R.id.r3c1);
ImageView r3c2=(ImageView)findViewById(R.id.r3c2);
ImageView r3c3=(ImageView)findViewById(R.id.r3c3);
ImageView mr1c1=(ImageView)findViewById(R.id.mr1c1);
ImageView mr1c2=(ImageView)findViewById(R.id.mr1c2);
ImageView mr1c3=(ImageView)findViewById(R.id.mr1c3);
ImageView mr2c1=(ImageView)findViewById(R.id.mr2c1);
ImageView mr2c2=(ImageView)findViewById(R.id.mr2c2);
ImageView mr2c3=(ImageView)findViewById(R.id.mr2c3);
ImageView mr3c1=(ImageView)findViewById(R.id.mr3c1);
ImageView mr3c2=(ImageView)findViewById(R.id.mr3c2);
ImageView mr3c3=(ImageView)findViewById(R.id.mr3c3);
int turn=1;


    //here goes my eraser
    r1c1.setAlpha(0f);
    r1c2.setAlpha(0f);
    r1c3.setAlpha(0f);
    r2c1.setAlpha(0f);
    r2c2.setAlpha(0f);
    r2c3.setAlpha(0f);
    r3c1.setAlpha(0f);
    r3c2.setAlpha(0f);
    r3c3.setAlpha(0f);
    mr1c1.setAlpha(0f);
    mr1c2.setAlpha(0f);
    mr1c3.setAlpha(0f);
    mr2c1.setAlpha(0f);
    mr2c2.setAlpha(0f);
    mr2c3.setAlpha(0f);
    mr3c1.setAlpha(0f);
    mr3c2.setAlpha(0f);
    mr3c3.setAlpha(0f);
}

答案 2 :(得分:0)

在您的onCreate方法中将(ImageView)findViewById(R.id.rici)初始化设置为setContentView(R.layout.activity_main)以下