如何以编程方式访问嵌套布局中的视图?

时间:2014-02-23 03:20:59

标签: java android xml android-layout

我正在尝试在我的Java代码中使用ImageView,该代码位于XML中的第二个RelativeLayout节点内。但是,我无法获得ImageViews方法所引用的setVisibility的引用,这种方法不会使图像不可见。

[DOUBLE EDIT]以下是我修订的活动类:

package com.scopelyapplication.tictactoe;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class TicTacToeActivity extends ActionBarActivity {

private static final String LOGTAG = "TicTacToeActivity";
private ModelComputerGameHelper comHelp;

boolean humanIsX;

RelativeLayout layout;
ImageView blueX;
ImageView greyX;
ImageView blueO;
ImageView greyO;

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

    layout = (RelativeLayout)findViewById(R.id.mainLayout);

    Log.d(LOGTAG, "onCreate");

    //Set up action bar
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayUseLogoEnabled(false);
    actionBar.setDisplayHomeAsUpEnabled(true);
    Log.d(LOGTAG, "set action bar");

    //Initialize X's and O's
    blueX = (ImageView)findViewById(R.id.blueXTopLeft);
    greyX = (ImageView)findViewById(R.id.greyXTopLeft);
    blueO = (ImageView)findViewById(R.id.blueOTopLeft);
    greyO = (ImageView)findViewById(R.id.greyOTopLeft);
    Log.d(LOGTAG, "X's and O's initialized");

    //Turn off their visibility until one is clicked.
    blueX.setVisibility(View.INVISIBLE); //where the NPE occurs
    greyX.setVisibility(View.INVISIBLE);
    blueO.setVisibility(View.INVISIBLE);
    greyO.setVisibility(View.INVISIBLE);

    Bundle extras = getIntent().getExtras();
    boolean gameIsPVP = extras.getBoolean("gameIsPVP");
    humanIsX = extras.getBoolean("humanIsX");

    if (gameIsPVP) {
        ; //in progress
    } else {
        startComputerGame(humanIsX);
    }
}

public void squareOneClick(View imageView) {

    char humanPlayer = ' ';
    if (humanIsX) {
        humanPlayer = 'X';
    } else {
        humanPlayer = 'O';
    }
    comHelp.makeMove(humanPlayer, 0);
    if (humanPlayer == 'X') {
        blueX.setVisibility(View.VISIBLE);
    } else {
        blueO.setVisibility(View.VISIBLE);
    }

    updateBoard();
}
public void squareTwoClick(View imageView) {

    char humanPlayer = ' ';
    if (humanIsX) {
        humanPlayer = 'X';
    }
    else {
        humanPlayer = 'O';
    }
    comHelp.makeMove(humanPlayer, 1);
    updateBoard();
}
public void squareThreeClick(View imageView) {

    char humanPlayer = ' ';
    if (humanIsX) {
        humanPlayer = 'X';
    }
    else {
        humanPlayer = 'O';
    }
    comHelp.makeMove(humanPlayer, 2);
    updateBoard();
}
public void squareFourClick(View imageView) {

    char humanPlayer = ' ';
    if (humanIsX) {
        humanPlayer = 'X';
    }
    else {
        humanPlayer = 'O';
    }
    comHelp.makeMove(humanPlayer, 3);
    updateBoard();
}
public void squareFiveClick(View imageView) {

    char humanPlayer = ' ';
    if (humanIsX) {
        humanPlayer = 'X';
    }
    else {
        humanPlayer = 'O';
    }
    comHelp.makeMove(humanPlayer, 4);
    updateBoard();
}
public void squareSixClick(View imageView) {

    char humanPlayer = ' ';
    if (humanIsX) {
        humanPlayer = 'X';
    }
    else {
        humanPlayer = 'O';
    }
    comHelp.makeMove(humanPlayer, 5);
    updateBoard();
}
public void squareSevenClick(View imageView) {

    char humanPlayer = ' ';
    if (humanIsX) {
        humanPlayer = 'X';
    }
    else {
        humanPlayer = 'O';
    }
    comHelp.makeMove(humanPlayer, 6);
    updateBoard();
}
public void squareEightClick(View imageView) {

    char humanPlayer = ' ';
    if (humanIsX) {
        humanPlayer = 'X';
    }
    else {
        humanPlayer = 'O';
    }
    comHelp.makeMove(humanPlayer, 7);
    updateBoard();
}
public void squareNineClick(View imageView) {

    char humanPlayer = ' ';
    if (humanIsX) {
        humanPlayer = 'X';
    }
    else {
        humanPlayer = 'O';
    }
    comHelp.makeMove(humanPlayer, 8);
    updateBoard();
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        finish();
        startActivity(new Intent(this, PickerActivity.class));
    }
    return super.onOptionsItemSelected(item);
}

public void startComputerGame(boolean isHumanX) {
    Log.d(LOGTAG, "startComputerGame");
    comHelp = new ModelComputerGameHelper(isHumanX);
    updateBoard();
}

public void updateBoard() {
    //in progress. connects startComputerGame and Clicks with Visuals.

}

}

以下是 activity_main 的一些XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="20dp"
android:minWidth="20dp" >

<ImageView
    android:id="@+id/gameBoard"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:contentDescription="@string/gameboard"
    android:src="@drawable/gameboard" />

<RelativeLayout
    android:id="@+id/squareOneLayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="290dp"
    android:layout_marginRight="220dp"
    android:layout_marginTop="70dp" >

    <ImageButton
        android:id="@+id/squareOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="288dp"
        android:layout_marginRight="219dp"
        android:layout_marginTop="71dp"
        android:background="@android:color/transparent"
        android:contentDescription="@string/top_left"
        android:onClick="squareOneClick"
        android:src="@drawable/blank_button" />

    <!-- Top-left images -->

    <ImageView
        android:id="@+id/blueOTopLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/greyOTopLeft"
        android:layout_centerVertical="true"
        android:src="@drawable/piece_o" />

    <ImageView
        android:id="@+id/greyOTopLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/piece_o_grey" />

    <ImageView
        android:id="@+id/greyXTopLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/blueOTopLeft"
        android:layout_centerVertical="true"
        android:src="@drawable/piece_x_grey" />

    <ImageView
        android:id="@+id/blueXTopLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/blueOTopLeft"
        android:layout_centerVertical="true"
        android:src="@drawable/piece_x" />
</RelativeLayout>

... <!-- rest of xml -->

</RelativeLayout>

这是我的logcat,抱歉遗忘!

02-23 03:27:26.984: D/AndroidRuntime(1125): Shutting down VM
02-23 03:27:26.984: E/AndroidRuntime(1125): FATAL EXCEPTION: main
02-23 03:27:26.984: E/AndroidRuntime(1125): Process: com.scopelyapplication.tictactoe, PID: 1125
02-23 03:27:26.984: E/AndroidRuntime(1125): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.scopelyapplication.tictactoe/com.scopelyapplication.tictactoe.TicTacToeActivity}: java.lang.NullPointerException
02-23 03:27:26.984: E/AndroidRuntime(1125):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
02-23 03:27:26.984: E/AndroidRuntime(1125):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-23 03:27:26.984: E/AndroidRuntime(1125):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-23 03:27:26.984: E/AndroidRuntime(1125):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-23 03:27:26.984: E/AndroidRuntime(1125):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-23 03:27:26.984: E/AndroidRuntime(1125):     at android.os.Looper.loop(Looper.java:136)
02-23 03:27:26.984: E/AndroidRuntime(1125):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-23 03:27:26.984: E/AndroidRuntime(1125):     at java.lang.reflect.Method.invokeNative(Native Method)
02-23 03:27:26.984: E/AndroidRuntime(1125):     at java.lang.reflect.Method.invoke(Method.java:515)
02-23 03:27:26.984: E/AndroidRuntime(1125):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-23 03:27:26.984: E/AndroidRuntime(1125):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-23 03:27:26.984: E/AndroidRuntime(1125):     at dalvik.system.NativeStart.main(Native Method)
02-23 03:27:26.984: E/AndroidRuntime(1125): Caused by: java.lang.NullPointerException
02-23 03:27:26.984: E/AndroidRuntime(1125):     at com.scopelyapplication.tictactoe.TicTacToeActivity.onCreate(TicTacToeActivity.java:54)
02-23 03:27:26.984: E/AndroidRuntime(1125):     at android.app.Activity.performCreate(Activity.java:5231)
02-23 03:27:26.984: E/AndroidRuntime(1125):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-23 03:27:26.984: E/AndroidRuntime(1125):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-23 03:27:26.984: E/AndroidRuntime(1125):     ... 11 more

谢谢!

4 个答案:

答案 0 :(得分:2)

只需使用其ID直接访问它 将行更改为:

blueX = (ImageView) findViewById(R.id.blueXTopLeft);

并与其他相关变量做类似的事情,然后就可以了。

答案 1 :(得分:1)

我认为您的问题在于布局XML。请参阅RelativeLayout的文档:

  

请注意,您不能在RelativeLayout的大小与其子项的位置之间存在循环依赖关系。例如,您不能将RelativeLayout的高度设置为WRAP_CONTENT,将子级设置为ALIGN_PARENT_BOTTOM。

您需要更正squareOneLayout中的循环依赖关系(也可能在其他地方)。

如果您希望子项相对于父RelativeLayout定位,则RelativeLayout无法根据子项的大小调整自身大小。

换句话说,如果子节点相对于父节点的大小(RelativeLayout)定位,则父节点(RelativeLayout)无法根据其子节点(ImageViews)的大小和整体布局来调整自身大小。结果是循环依赖,无法解决。

您需要更改RelativeLayout的大小,或者将ImageViews相对于同级视图(例如其中一个ImageViews)定位,而不是相对于父RelativeLayout定位。

答案 2 :(得分:0)

如前所述,您是否尝试过清理项目?你的R.java可能有点搞砸了。项目&GT;清洁

答案 3 :(得分:0)

试试这个......

<强> activity_main:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="20dp"
android:minWidth="20dp" >

<ImageView
  android:id="@+id/gameBoard"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_alignParentBottom="true"
  android:layout_alignParentLeft="true"
  android:layout_alignParentRight="true"
  android:layout_alignParentTop="true"
  android:contentDescription="@string/gameboard"
  android:src="@drawable/gameboard" />

<RelativeLayout
  android:id="@+id/squareOneLayout"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginBottom="290dp"
  android:layout_marginRight="220dp"
  android:layout_marginTop="70dp" >

  <ImageButton
    android:id="@+id/squareOne"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="288dp"
    android:layout_marginRight="219dp"
    android:layout_marginTop="71dp"
    android:background="@android:color/transparent"
    android:contentDescription="@string/top_left"
    android:onClick="squareOneClick"
    android:src="@drawable/blank_button" />

<!-- Top-left images -->

<ImageView
    android:id="@+id/blueOTopLeft"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/greyOTopLeft"
    android:layout_centerVertical="true"
    android:src="@drawable/piece_o" 
    android:visibility="invisible"/>

<ImageView
    android:id="@+id/greyOTopLeft"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:src="@drawable/piece_o_grey" 
    android:visibility="invisible"/>

<ImageView
    android:id="@+id/greyXTopLeft"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/blueOTopLeft"
    android:layout_centerVertical="true"
    android:src="@drawable/piece_x_grey" 
    android:visibility="invisible"
   />

<ImageView
    android:id="@+id/blueXTopLeft"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/blueOTopLeft"
    android:layout_centerVertical="true"
    android:src="@drawable/piece_x" 
    android:visibility="invisible"/>
  </RelativeLayout>

  ... <!-- rest of xml -->

  </RelativeLayout>

<强> MainActivity.java

package com.scopelyapplication.tictactoe;

 import android.content.Intent;
 import android.os.Bundle;
 import android.support.v7.app.ActionBar;
 import android.support.v7.app.ActionBarActivity;
 import android.util.Log;
 import android.view.MenuItem;
 import android.view.View;
 import android.widget.ImageButton;
 import android.widget.ImageView;
 import android.widget.RelativeLayout;

    public class TicTacToeActivity extends ActionBarActivity {

    private static final String LOGTAG = "TicTacToeActivity";
    private ModelComputerGameHelper comHelp;

    boolean humanIsX;

    RelativeLayout layout;
    ImageView blueX, greyX, blueO, greyO;

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

layout = (RelativeLayout)findViewById(R.id.mainLayout);

Log.d(LOGTAG, "onCreate");

//Set up action bar
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayUseLogoEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
Log.d(LOGTAG, "set action bar");

//Initialize X's and O's
blueX = (ImageView)findViewById(R.id.blueXTopLeft);
greyX = (ImageView)findViewById(R.id.greyXTopLeft);
blueO = (ImageView)findViewById(R.id.blueOTopLeft);
greyO = (ImageView)findViewById(R.id.greyOTopLeft);
Log.d(LOGTAG, "X's and O's initialized");



Bundle extras = getIntent().getExtras();
boolean gameIsPVP = extras.getBoolean("gameIsPVP");
humanIsX = extras.getBoolean("humanIsX");

if (gameIsPVP) {
    ; //in progress
} else {
    startComputerGame(humanIsX);
}
}

public void squareOneClick(View imageView) {

char humanPlayer = ' ';
if (humanIsX) {
    humanPlayer = 'X';
} else {
    humanPlayer = 'O';
}
comHelp.makeMove(humanPlayer, 0);
if (humanPlayer == 'X') {
    blueX.setVisibility(View.VISIBLE);
} else {
    blueO.setVisibility(View.VISIBLE);
}

 updateBoard();
 }
 public void squareTwoClick(View imageView) {

char humanPlayer = ' ';
if (humanIsX) {
    humanPlayer = 'X';
}
else {
    humanPlayer = 'O';
}
comHelp.makeMove(humanPlayer, 1);
updateBoard();
}
public void squareThreeClick(View imageView) {

char humanPlayer = ' ';
if (humanIsX) {
    humanPlayer = 'X';
}
else {
    humanPlayer = 'O';
}
comHelp.makeMove(humanPlayer, 2);
updateBoard();
}
public void squareFourClick(View imageView) {

char humanPlayer = ' ';
if (humanIsX) {
    humanPlayer = 'X';
}
else {
    humanPlayer = 'O';
}
comHelp.makeMove(humanPlayer, 3);
updateBoard();
}
public void squareFiveClick(View imageView) {

char humanPlayer = ' ';
if (humanIsX) {
    humanPlayer = 'X';
}
else {
    humanPlayer = 'O';
}
comHelp.makeMove(humanPlayer, 4);
updateBoard();
}
public void squareSixClick(View imageView) {

char humanPlayer = ' ';
if (humanIsX) {
    humanPlayer = 'X';
}
else {
    humanPlayer = 'O';
}
 comHelp.makeMove(humanPlayer, 5);
 updateBoard();
 }
public void squareSevenClick(View imageView) {

char humanPlayer = ' ';
if (humanIsX) {
    humanPlayer = 'X';
}
else {
    humanPlayer = 'O';
}
comHelp.makeMove(humanPlayer, 6);
updateBoard();
}
public void squareEightClick(View imageView) {

char humanPlayer = ' ';
if (humanIsX) {
    humanPlayer = 'X';
}
else {
    humanPlayer = 'O';
}
comHelp.makeMove(humanPlayer, 7);
updateBoard();
}
public void squareNineClick(View imageView) {

char humanPlayer = ' ';
if (humanIsX) {
    humanPlayer = 'X';
}
else {
    humanPlayer = 'O';
}
comHelp.makeMove(humanPlayer, 8);
 updateBoard();
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
    finish();
    startActivity(new Intent(this, PickerActivity.class));
}
return super.onOptionsItemSelected(item);
}

public void startComputerGame(boolean isHumanX) {
Log.d(LOGTAG, "startComputerGame");
comHelp = new ModelComputerGameHelper(isHumanX);
 updateBoard();
 }

public void updateBoard() {
//in progress. connects startComputerGame and Clicks with Visuals.

}

}