我正在使用CourtCounter应用程序,但我有3个错误 1)当你试图为Peat添加一个点时,它不起作用。所有积分都是针对Marta的账户 2)当我按下Reset按钮时,整个应用程序正在重启(只是它正在停止)。我认为应该重新启动结果 3)背景图像 - 正如你所看到的,它被扩展了。它看起来并不好看。我想我只需要使用尺寸合适的专用于手机的图片? enter image description here
**** **代码
** **** activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@drawable/a"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
tools:context="com.example.majka.courtcounter.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="4dp"
android:text="MARTA'S TEAM"
android:textColor="#FF040510"
android:background="#c2c2c6"
android:textSize="20sp"
android:textStyle="bold"
android:fontFamily="sans-serif"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:layout_marginRight="2dp"
android:elevation="2dp"/>
<TextView
android:id="@+id/martas_team_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="4dp"
android:text="0"
android:textSize="80sp"
android:paddingBottom="24dp"
android:paddingTop="16dp"
android:textColor="#000000"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="addThreeToTeamA"
android:text="+3 points"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="8dp"
android:background="#3F51B5"
android:textColor="#ECEFF1"
android:layout_marginTop="13dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="addTwoToTeamA"
android:text="+2 points"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="8dp"
android:background="#3F51B5"
android:textColor="#ECEFF1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="addOneToTeamA"
android:text="Free Throw"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="8dp"
android:background="#3F51B5"
android:textColor="#ECEFF1"/>
</LinearLayout>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
tools:context="com.example.majka.courtcounter.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="4dp"
android:text=" PEAT'S TEAM"
android:textColor="#040510"
android:background="#c2c2c6"
android:textSize="20sp"
android:textStyle="bold"
android:fontFamily="sans-serif"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="2dp"
android:elevation="2dp"/>
<TextView
android:id="@+id/team_2_scores"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="4dp"
android:text="0"
android:textSize="80sp"
android:paddingBottom="24dp"
android:paddingTop="16dp"
android:textColor="#000000"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="addThreeToTeamA"
android:text="+3 points"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="8dp"
android:background="#673AB7"
android:textColor="#ECEFF1"
android:layout_marginTop="13dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="addTwoToTeamA"
android:text="+2 points"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="8dp"
android:background="#673AB7"
android:textColor="#ECEFF1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:onClick="addOneToTeamA"
android:text="Free Throw"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginBottom="8dp"
android:background="#673AB7"
android:textColor="#ECEFF1"/>
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RESET"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="ResetScore"
android:background="#B0BEC5"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"/>
</RelativeLayout>
mainActivity
package com.example.majka.courtcounter;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int scoreTeamA = 0;
int scoreTeamB = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayForTeamA(0);
}
public void addThreeToTeamA (View v ) {
scoreTeamA = scoreTeamA + 3;
displayForTeamA(scoreTeamA);
}
public void addTwoToTeamA (View v ) {
scoreTeamA = scoreTeamA + 2;
displayForTeamA(scoreTeamA);
}
public void addOneToTeamA (View v ) {
scoreTeamA = scoreTeamA + 1;
displayForTeamA(scoreTeamA);
}
public void addOneForTeamB(View v) {
scoreTeamB = scoreTeamB + 1;
displayForTeamB(scoreTeamB);
}
/**
* Increase the score for Team B by 2 points.
*/
public void addTwoForTeamB(View v) {
scoreTeamB = scoreTeamB + 2;
displayForTeamB(scoreTeamB);
}
/**
* Increase the score for Team B by 3 points.
*/
public void addThreeForTeamB(View v) {
scoreTeamB = scoreTeamB + 3;
displayForTeamB(scoreTeamB);
}
/**
* Displays the given score for Team A.
*/
public void displayForTeamA(int score) {
TextView scoreView = (TextView) findViewById(R.id.martas_team_score);
scoreView.setText(String.valueOf(score));
}
/**
* Displays the given score for Team B.
*/
public void displayForTeamB(int score) {
TextView scoreView = (TextView) findViewById(R.id.peats_team_score);
scoreView.setText(String.valueOf(score));
}
public void resetScore(View v) {
scoreTeamA = 0;
scoreTeamB = 0;
displayForTeamA(scoreTeamA);
displayForTeamB(scoreTeamB);
}
}
任何人都可以帮助我吗? 我没有把错误看成代码。我不知道它为什么不起作用。
答案 0 :(得分:0)
1)在Peat团队的按钮中,onClick
属性正在使用A队的方法。
2)您的重置按钮的onClick
属性引用了一个名为ResetScore
的方法,但您在代码中定义的方法名为resetScore
(请注意小写的“r”)。这会导致您的应用崩溃。
3)您可能希望使用其他图像,或者,如果切换到使用android:src
属性,则可以使用android:scaleType
属性控制图像的缩放。