目前我的Java代码存在两个问题。
我在这里附上了代码。
基本上,它抱怨我试图解决的模糊方法调用,但似乎无法解决,而且显示B队的分数也存在问题。< / p>
帮助?这非常棘手,因为尽管代码完全相同,但我遇到了不同的问题。
package com.example.android.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 {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* Increase the score for Team A by 1 point.
*/
public void addOneForTeamA(View v) {
displayForTeamA(1);
}
/**
* Increase the score for Team A by 2 points.
*/
public void addTwoForTeamA(View v) {
displayForTeamA(2);
}
/**
* Increase the score for Team A by 3 points.
*/
public void addThreeForTeamA(View v) {
displayForTeamA(3);
}
public void displayForTeamA(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_a_score);
scoreView.setText(String.valueOf(score));
}
/**
* Displays the given score for Team A.
*/
public void displayForTeamA(int score) {
TextView scoreView = findViewById(R.id.team_a_score);
scoreView.setText(String.valueOf(score));
}
/**
* Increase the score for Team B by 1 point.
*/
public void addOneForTeamB(View v) {
displayForTeamB(1);
}
/**
* Increase the score for Team B by 2 points.
*/
public void addTwoForTeamB(View v) {
displayForTeamB(2);
}
/**
* Increase the score for Team B by 3 points.
*/
public void addThreeForTeamB(View v) {
displayForTeamB(3);
}
/**
* Displays the given score for Team B.
*/
public void displayForTeamA(int score) {
TextView scoreView = findViewById(R.id.team_b_score);
scoreView.setText(String.valueOf(score));
}
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="173dp"
android:layout_height="47dp"
android:layout_alignParentEnd="true"
android:layout_below="@+id/textView"
android:gravity="center"
android:padding="4dp"
android:text="0"
android:id="@id/team_b_score"
tools:ignore="RtlCompat" />
<TextView
android:id="@+id/textView"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="65dp"
android:layout_marginTop="26dp"
android:text="Team A"
android:layout_alignParentLeft="true"
tools:ignore="RtlCompat" />
<TextView
android:id="@+id/textView2"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignTop="@+id/textView"
android:layout_marginEnd="35dp"
android:text="Team B"
tools:ignore="RtlCompat" />
<TextView
android:layout_width="173dp"
android:layout_height="47dp"
android:layout_alignParentStart="true"
android:layout_below="@+id/textView"
android:gravity="center"
android:padding="4dp"
android:text="0"
android:id="@+id/team_a_score"
tools:ignore="RtlCompat" />
<Button
android:id="@+id/button17"
android:layout_width="118dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="114dp"
android:background="@android:color/holo_orange_dark"
android:text="RESET"
android:onClick="resetButton"/>
<Button
android:id="@+id/button12"
android:layout_width="129dp"
android:layout_height="34dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/button10"
android:layout_gravity="clip_vertical"
android:layout_marginEnd="26dp"
android:layout_marginRight="26dp"
android:background="@android:color/holo_orange_dark"
android:baselineAligned="false"
android:text="+3 Points"
android:onClick="addThreeForTeamB"
tools:ignore="RtlCompat" />
<Button
android:id="@+id/button10"
android:layout_width="129dp"
android:layout_height="34dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/button11"
android:layout_gravity="clip_vertical"
android:layout_marginRight="232dp"
android:layout_marginTop="117dp"
android:background="@android:color/holo_orange_dark"
android:baselineAligned="false"
android:text="+3 Points"
android:onClick="addThreeForTeamA"
tools:ignore="RtlCompat" />
<Button
android:id="@+id/button11"
android:layout_width="129dp"
android:layout_height="34dp"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_gravity="clip_vertical"
android:layout_marginStart="25dp"
android:layout_marginTop="171dp"
android:background="@android:color/holo_orange_dark"
android:baselineAligned="false"
android:text="Button"
tools:ignore="RtlCompat"
tools:text="+2 Points"
android:onClick="addTwoForTeamA"
/>
<Button
android:id="@+id/button15"
android:layout_width="129dp"
android:layout_height="34dp"
android:layout_alignParentRight="true"
android:layout_alignStart="@+id/button4"
android:layout_alignTop="@+id/button11"
android:layout_gravity="clip_vertical"
android:layout_marginRight="26dp"
android:background="@android:color/holo_orange_dark"
android:baselineAligned="false"
android:text="Button"
tools:ignore="RtlCompat"
tools:text="+2 Points"
android:onClick="addTwoForTeamB"/>
<Button
android:id="@+id/button4"
android:layout_width="129dp"
android:layout_height="34dp"
android:layout_alignEnd="@+id/button12"
android:layout_alignTop="@+id/button14"
android:layout_gravity="left"
android:background="@android:color/holo_orange_dark"
android:baselineAligned="false"
android:text="FREE THROW"
tools:ignore="RtlCompat"
android:onClick="addOneForTeamB"/>
<Button
android:id="@+id/button14"
android:layout_width="129dp"
android:layout_height="34dp"
android:layout_alignParentTop="true"
android:layout_alignStart="@+id/button11"
android:layout_gravity="left"
android:layout_marginTop="223dp"
android:background="@android:color/holo_orange_dark"
android:baselineAligned="false"
android:text="FREE THROW"
tools:ignore="RtlCompat"
android:onClick="addOneForTeamA"/>
</RelativeLayout>
答案 0 :(得分:1)
它抱怨暧昧的代码,因为你有三次写同样的方法:
public void displayForTeamA(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_a_score);
scoreView.setText(String.valueOf(score));
}
/**
* Displays the given score for Team A.
*/
public void displayForTeamA(int score) {
TextView scoreView = findViewById(R.id.team_a_score);
scoreView.setText(String.valueOf(score));
}
然后吼道:
/**
* Displays the given score for Team B.
*/
public void displayForTeamA(int score) {
TextView scoreView = findViewById(R.id.team_b_score);
scoreView.setText(String.valueOf(score));
}
删除重复的定义。