您好,我尝试在手机上通过USB运行该应用程序,只是为了确保它可以正常工作,并且出现了此错误。
当我更深入地研究错误时,它提到了有关XML文件底部的centerHorizontal的内容。
尝试了一些无效的解决方案。任何帮助都很好。非常感谢你!
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:paddingBottom="0dp"
android:paddingTop="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
tools:context=".MainActivity">
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="@layout/buttons"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:id="@+id/include"/>
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="@layout/hands"
android:layout_centerHoriztonal="true"
android:layout_alignParentTop="true"/>
package com.example.rockpaper;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
Button b_rock, b_paper, b_scissors;
ImageView iv_cpu, iv_me;
String myChoice, cpuChoice, result;
Random r;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_cpu = (ImageView) findViewById(R.id.iv_cpu);
iv_me = (ImageView) findViewById(R.id.iv_me);
b_rock = (Button) findViewById(R.id.b_rock);
b_paper = (Button) findViewById(R.id.b_paper);
b_scissors = (Button) findViewById(R.id.b_scissors);
r = new Random();
b_rock.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myChoice = "rock";
iv_cpu.setImageResource(R.drawable.rock);
calculate();
}
});
b_paper.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myChoice = "paper";
iv_cpu.setImageResource(R.drawable.paper);
calculate();
}
});
b_scissors.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
myChoice = "scissors";
iv_cpu.setImageResource(R.drawable.scissors);
calculate();
}
});
}
public void calculate() {
int cpu = r.nextInt(3);
if (cpu == 0) {
cpuChoice = "rock";
iv_cpu.setImageResource(R.drawable.rock);
} else if (cpu == 1) {
cpuChoice = "paper";
iv_cpu.setImageResource(R.drawable.paper);
} else if (cpu == 2) {
cpuChoice = "scissors";
iv_cpu.setImageResource(R.drawable.scissors);
}
if(myChoice.equals("rock") && cpuChoice.equals("paper")){
result = "you lose";
} else
if(myChoice.equals("rock") && cpuChoice.equals("scissors")){
result = "you win";
} else
if(myChoice.equals("rock") && cpuChoice.equals("rock")){
result = "draw";
} else
if(myChoice.equals("paper") && cpuChoice.equals("paper")){
result = "draw";
} else
if(myChoice.equals("paper") && cpuChoice.equals("scissors")){
result = "you lose";
} else
if(myChoice.equals("paper") && cpuChoice.equals("rock")){
result = "you win";
} else
if(myChoice.equals("scissors") && cpuChoice.equals("paper")){
result = "you win";
} else
if(myChoice.equals("scissors") && cpuChoice.equals("scissors")){
result = "draw";
} else
if(myChoice.equals("scissors") && cpuChoice.equals("rock")){
result = "you lose";
}
Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
}
}
更详细的错误消息
答案 0 :(得分:0)
您的根布局为ConstraintLayout
,但这些
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
属性属于RelativeLayout
解决方案
将根布局更改为RelativeLayout
或在ConstraintLayout
标签内指定include
的相应定位元素
喜欢
app:layout_constraintLeft_toRightOf = "id or parent"
app:layout_constraintRight_toLeftOf = "id or parent"
app:layout_constraintTop_toTopOf = "id or parent"
app:layout_constraintBottom_toBottomOf = "id or parent"