Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java.lang运行时异常:无法在我的应用程序中实例化活动组件信息错误_Java_Android_Android Layout - Fatal编程技术网

Java.lang运行时异常:无法在我的应用程序中实例化活动组件信息错误

Java.lang运行时异常:无法在我的应用程序中实例化活动组件信息错误,java,android,android-layout,Java,Android,Android Layout,我正在学习Android开发课程,其中一个练习是制作一个简单的Connect 3游戏。我不确定错误在哪里,因为它不是编译错误,所以我不知道在哪里更改代码 以下是MainActivity.java代码: package com.anurag.connectthree; import android.media.Image; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import androi

我正在学习Android开发课程,其中一个练习是制作一个简单的Connect 3游戏。我不确定错误在哪里,因为它不是编译错误,所以我不知道在哪里更改代码

以下是MainActivity.java代码:

package com.anurag.connectthree;
import android.media.Image;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

// 0 = yellow, 1 = red  => means yellow goes first
int activePlayer = 0;

//save a memory state to know when a tile has been clicked
int[] gameState = {2,2,2,2,2,2,2,2,2};

//check if game is won after min 5 moves
static int noOfMoves = 0;

//layout over the grid layout for playing again
private LinearLayout playAgainLayout = (LinearLayout)findViewById(R.id.playAgainLayout);

//the winner message on the linear  layout
private TextView winnerMessage = (TextView)findViewById(R.id.winnerMessage);

//initially true, set to false after game has been won
private boolean gameIsActive = true;

public void dropIn(View view){


    //tapped on view, so imageview is the view itself, then set resource to image as done on line 31 and 35
    ImageView counter =(ImageView) view;

    //Added tags to tiles in XML .getTag() returns tag and can act as index of gameState array
    int tappedCounter = Integer.parseInt(counter.getTag().toString());

    //2 means tile hasn't been been clicked
    if(gameState[tappedCounter] == 2 && gameIsActive) {

        gameState[tappedCounter]=activePlayer; //this will tell us which player is playing this move

        counter.setTranslationY(-1000f);

        if (activePlayer == 0) {
            counter.setImageResource(R.drawable.yellow);
            activePlayer = 1;
        } else {
            counter.setImageResource(R.drawable.red);
            activePlayer = 0;
        }
        counter.animate().translationYBy(1000f).rotation(540f).setDuration(300);

    }

    noOfMoves++;

    Log.i("Move number ",Integer.toString(noOfMoves));

    if(noOfMoves>=5) checkForWin();

}

private void checkForWin() {
    int i,j;

    //winning combinations are {{0,1,2},{3,4,5},{6,7,8}} horizontally, {{0,3,6},{1,4,7},{2,5,8}} vertically and {{0,4,8}, {2,4,6}} diagonally
    for(i=1,j=3;i<=8&&j<=8;i+=3,j+=1){
        if((gameState[i-1]==gameState[i]&&gameState[i+1]==gameState[i])    //takes care of horizontal
                ||(gameState[j-3]==gameState[j]&&gameState[j]==gameState[j+3])   //takes care of vertical
            ||(gameState[0]==gameState[4]&&gameState[4]==gameState[8])       //hardcoding diagonal
                ||(gameState[2]==gameState[4]&&gameState[4]==gameState[6])){



            if(gameState[i]==0||gameState[j]==0||gameState[4]==0) {
                gameIsActive = false;
                winnerMessage.setText("Yellow has won!");
                if(playAgainLayout.getVisibility()==View.INVISIBLE)
                    playAgainLayout.setVisibility(View.VISIBLE);

            }
            else if(gameState[i]==1||gameState[j]==1||gameState[4]==1) {
                gameIsActive = false;
                winnerMessage.setText("Yellow has won!");
                if(playAgainLayout.getVisibility()==View.INVISIBLE)
                    playAgainLayout.setVisibility(View.VISIBLE);

            }
        }
    }

}
public void playAgain(View view){

    //for playing again set the game to active again
    gameIsActive=true;

    //this is again set to invisible
    playAgainLayout.setVisibility(View.INVISIBLE);

    //game state reset to {2,2,2,2,2,2,2,2,2}
    for(int i =0; i<gameState.length;i++)
        gameState[i]=2;

    //again yellow will start, so set to 0
    activePlayer = 0;

    GridLayout gridLayout = (GridLayout)findViewById(R.id.gridLayout);

    //for each of the 9 children of the grid, set their image src to null
    for(int i=0;i<gridLayout.getChildCount();i++){

        ((ImageView) gridLayout.getChildAt(i)).setImageResource(0);
    }

}


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

    Log.i("Check if game starts :","Yes");

    }
}
package com.anurag.connectthree;
导入android.media.Image;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.GridLayout;
导入android.widget.ImageView;
导入android.widget.LinearLayout;
导入android.widget.TextView;
公共类MainActivity扩展了AppCompatActivity{
//0=黄色,1=红色=>表示黄色优先
int activePlayer=0;
//保存内存状态以了解单击磁贴的时间
int[]配子状态={2,2,2,2,2,2,2,2};
//检查游戏是否在最少5步后获胜
静态int noOfMoves=0;
//布局在网格布局上,以便再次播放
专用LinearLayout playAgainLayout=(LinearLayout)findViewById(R.id.playAgainLayout);
//线性布局上的赢家消息
私有TextView WinneMessage=(TextView)findViewById(R.id.WinneMessage);
//最初为true,在游戏获胜后设置为false
私有布尔gameIsActive=true;
公共void dropIn(视图){
//点击view,使imageview成为视图本身,然后将resource设置为image,如第31行和第35行所示
ImageView计数器=(ImageView)视图;
//将标记添加到XML中的平铺中。getTag()返回标记,并可以作为游戏状态数组的索引
int-tappedCounter=Integer.parseInt(counter.getTag().toString());
//2表示尚未单击磁贴
如果(游戏状态[tappedCounter]==2&&gameIsActive){
gameState[tappedCounter]=activePlayer;//这将告诉我们哪个玩家正在玩这个动作
计数器设置平移Y(-1000f);
如果(activePlayer==0){
计数器.setImageResource(R.drawable.yellow);
activePlayer=1;
}否则{
计数器.setImageResource(R.drawable.red);
activePlayer=0;
}
counter.animate();
}
noOfMoves++;
Log.i(“移动编号”,Integer.toString(noOfMoves));
如果(noOfMoves>=5),则检查forwin();
}
私有void checkForWin(){
int i,j;
//获胜组合为{0,1,2}、{3,4,5}、{6,7,8}水平、{0,3,6}、{1,4,7}、{2,5,8}垂直和{0,4,8}、{2,4,6}对角

对于(i=1,j=3;i视图应该在方法
onCreate()
中找到,在
setContentView()之后

您有两个视图(playAgainLayout和WinneMessage)正在初始化,作为使用
findViewById
API调用的字段声明的一部分

调用此类的构造函数时,首先初始化这些字段。 这可以通过堆栈跟踪进行验证:

at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.anurag.connectthree.MainActivity.<init>(MainActivity.java:26)
希望这有帮助

<?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=".MainActivity">

<android.support.v7.widget.GridLayout
    android:id="@+id/gridLayout"
    android:layout_width="369dp"
    android:layout_height="424dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:background="@drawable/board"
    app:columnCount="3"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.492"
    app:rowCount="3">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="119dp"
        android:layout_height="134dp"

        android:onClick="dropIn"
        android:tag="0" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="119dp"
        android:layout_height="134dp"
        android:layout_marginLeft="10dp"
        android:onClick="dropIn"
        android:tag="1"
        app:layout_column="1"
        app:layout_row="0" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="119dp"
        android:layout_height="134dp"
        android:layout_marginLeft="5dp"
        android:onClick="dropIn"
        android:tag="2" />

    <ImageView
        android:id="@+id/imageView10"
        android:layout_width="119dp"
        android:layout_height="134dp"
        android:layout_marginTop="5dp"
        android:onClick="dropIn"
        android:tag="3" />

    <ImageView
        android:id="@+id/imageView9"
        android:layout_width="119dp"
        android:layout_height="134dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="5dp"
        android:onClick="dropIn"
        android:tag="4" />

    <ImageView
        android:id="@+id/imageView8"
        android:layout_width="119dp"
        android:layout_height="134dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:onClick="dropIn"
        android:tag="5" />

    <ImageView
        android:id="@+id/imageView7"
        android:layout_width="119dp"
        android:layout_height="134dp"
        android:layout_marginTop="13dp"
        android:onClick="dropIn"
        android:tag="6" />

    <ImageView
        android:id="@+id/imageView6"
        android:layout_width="119dp"
        android:layout_height="134dp"
        android:layout_marginLeft="9dp"
        android:layout_marginTop="13dp"
        android:onClick="dropIn"
        android:tag="7" />

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="119dp"
        android:layout_height="134dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="13dp"
        android:onClick="dropIn"
        android:tag="8" />

</android.support.v7.widget.GridLayout>

<LinearLayout
    android:id="@+id/playAgainLayout"
    android:layout_width="309dp"
    android:layout_height="179dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:background="@android:color/holo_blue_dark"
    android:orientation="vertical"
    android:padding="10dp"
    android:visibility="invisible"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:id="@+id/winnerMessage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:padding="10dp"
        android:text="The winner is "
        android:textColor="@android:color/black"
        android:textSize="30sp" />

    <Button
        android:id="@+id/playAgainButton"
        android:layout_width="141dp"
        android:layout_height="76dp"
        android:layout_gravity="center_vertical|center"
        android:onClick="playAgain"
        android:text="Play Again"
        android:textSize="17sp" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.anurag.connectthree">

<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/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
   private LinearLayout playAgainLayou
   private TextView winnerMessage

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        winnerMessage = (TextView)findViewById(R.id.winnerMessage);
        playAgainLayout =(LinearLayout)findViewById(R.id.playAgainLayout);
    }
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.anurag.connectthree.MainActivity.<init>(MainActivity.java:26)
//layout over the grid layout for playing again
private LinearLayout playAgainLayout;

//the winner message on the linear  layout
private TextView winnerMessage;

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

    // layout is inflated, now we can find the views using findViewById
    playAgainLayout = (LinearLayout)findViewById(R.id.playAgainLayout);
    winnerMessage = (TextView)findViewById(R.id.winnerMessage);

    Log.i("Check if game starts :","Yes");
    }
}