2015-12-01 71 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」 –

相關問題