2015-12-01 72 views
0

嗨,我是新的android开发,我不能执行方法时,单击按钮。我重新输入代码,如在教程中,但它结束了很多错误。检查下面的代码,无法执行android的方法:onClick

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Basketball Score Game" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:textSize="25sp" 
    android:layout_marginLeft="10dp" 
    android:layout_marginTop="10dp" 
    android:paddingLeft="70dp" 
    android:id="@+id/header" 
    /> 


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Team A" 
     android:layout_marginLeft="50dp" 
     android:layout_marginTop="80dp" 
     android:textSize="30sp" 
     /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="0" 
     android:textSize="50sp" 
     android:id="@+id/score_a" 
     android:layout_marginLeft="90dp" 
     android:layout_marginTop="10dp" 
     android:onClick="display_score_a" 
     /> 
    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#ff6000" 
     android:text="Outside The Ring" 
     android:onClick="three_pts_a" 
     android:padding="10dp" 
     android:layout_marginLeft="40dp" 
     android:layout_marginTop="40dp" 
     /> 
    <Button 
     android:layout_width="wrap_content" 
     android:background="#ff6000" 
     android:layout_height="wrap_content" 
     android:text="Inside the Ring" 
     android:layout_marginLeft="40dp" 
     android:padding="10dp" 
     android:layout_marginTop="10dp" 
     /> 
    <Button 
     android:layout_width="wrap_content" 
     android:background="#ff6000" 
     android:text="Free Throw" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="40dp" 
     android:layout_marginTop="10dp" 
     android:padding="10dp" 
     /> 
</LinearLayout> 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Team B" 
     android:layout_marginLeft="250dp" 
     android:layout_marginTop="80dp" 
     android:textSize="30sp" 
     /> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="0" 
     android:textSize="50sp" 
     android:layout_marginLeft="290dp" 
     android:layout_marginTop="10dp" 
     /> 


    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#ff6000" 
     android:text="Outside The Ring" 
     android:layout_marginLeft="230dp" 
     android:padding="10dp" 
     android:layout_marginTop="40dp" 
     /> 
    <Button 
     android:layout_width="wrap_content" 
     android:background="#ff6000" 
     android:text="Inside the Ring" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="230dp" 
     android:layout_marginTop="10dp" 
     android:padding="10dp" 
     /> 
    <Button 
     android:layout_width="wrap_content" 
     android:background="#ff6000" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="230dp" 
     android:layout_marginTop="10dp" 
     android:text="Free Throw" 
     android:padding="10dp" 
     /> 


</LinearLayout> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Reset" 
    android:background="#ff6000" 

    android:layout_gravity="center_horizontal" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" /> 

**** **** Mainactivity.java

package android.mytest; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.TextView; 
public class MainActivity extends AppCompatActivity { 
       int total_pts1; 

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

public void three_pts_a(View view) 
{ 

    total_pts1 = total_pts1 + 3; 
    display_score_a(total_pts1); 
} 

private void display_score_a(int number) { 
    TextView num = (TextView) findViewById(
      R.id.score_a); 
    num.setText(number); 
} 

} 
+1

可能由于'num.setText(number);'行导致'Resources $ NotFoundException'。使用'num.setText(String.valueOf(number));' –

+0

Thx它的工作:)对不起,这些跛脚的问题我Imma新手 –

回答

0

它,因为整数variable.you必须将它解析为一个字符串或将该行更改为num.setText(String.valueOf(number));

+0

错了,它是一个“int” –

相关问题