2016-05-25 10 views
0

私はAndroid(Java)を初めて使いました。このレッスンの1つは、このシナリオを作成することです。 Androidビュー内でクラスメソッドを使用できますか?

彼らはボタンごとに異なる方法を使用することを教えていますが、それはほぼ等しい2つの方法を追加し、DRYの心はそれが最良の解決策ではないと言います。

翔私はBasketTeamクラス書いた:

package com.example.android.courtcounter; 

import android.view.View; 
import android.widget.TextView; 

public class BasketTeam { 

    private TextView team; 

    public BasketTeam(View v) { 
     team = (TextView) v; 
    } 

    public void threePointsThrow(View v) { 
     this.addPoints(3); 
    } 

    public void twoPointsThrow(View v) { 
     addPoints(2); 
    } 

    public void freeThrow(View v) { 
     addPoints(1); 
    } 

    private void addPoints(Integer addedScore) { 
     Integer teamScore = Integer.parseInt((String) team.getText()); 
     team.setText("" + (teamScore + addedScore)); 
    } 

    public void resetScore() { 
     team.setText("" + 0); 
    } 
} 

をそして、私のMainActivityで私は公共BasketTeam1BasketTeam2として2つのインスタンスを作成しました。私MainActivityで、私は方法がそれらを使用resetScoreと呼ばれる持っているので 彼らは大丈夫ともインスタンス化の両方であり、それが動作します:

public void resetScore(View v) { 
    BasketTeam1.resetScore(); 
    BasketTeam2.resetScore(); 
} 

しかし、私は私の見解では、これらのクラスのメソッドのいずれかを使用しようとすると、それができません見つける。 なぜですか?ここで

私の試みの例である:

<Button 
    android:layout_height="wrap_content" 
    android:layout_width="120dp" 
    android:text="+3 points" 
    android:layout_gravity="center_horizontal" 
    android:layout_marginTop="16dp" 
    android:id="@+id/team_1_3_points" 
    android:background="@color/colorAccent" 
    android:textColor="@android:color/white" 
    android:onClick="BasketTeam1.threePointsThrow"/> 

更新

これはエラーメッセージです:

5月25日13:30:19.880 2345年から2345年/ com.example.android.courtcounter E/AndroidRuntime:致命的除外:メイン プロセス:com.example.android.courtcounter、PID:2345 java.lang.IllegalStateException:アンドロイドの親や先祖コンテキスト方法BasketTeam1.threePointsThrow(ビュー)を見つけることができませんでした:のonClick属性はidを持つビュークラスandroid.support.v7.widget.AppCompatButtonに定義された「team_1_3_points」

+0

私はあなたのビューのコードをポストする必要があると思います。 –

+0

あなたは実際にテキストビューにスコアを保存するという特有の種類で​​す。私のMVC-Mindは、モデル内のデータを保存し、ビューを更新時に更新する方が良いと言います...もう一つのポイント:なぜあなたの 'xxxPointsThrow'メソッドにViewを渡すのですか?あなたはそれを使用していないようです... – Fildor

+0

上記のコメントの最後の部分をしないでください。彼らはonClickHandlersです... – Fildor

答えて

2

ボタンのonClickプロパティは、ボタンのコンテキスト内のメソッド(通常はMainActivity)に対応する必要があります。 onClickを参照してください。

ソリューションは次のようになります。

レイアウトで
public class MainActivity extends Activity { 
    ... 
    public void threePointsThrow(View v) { 
     switch (v.getId()) { 
      case R.id.team_1_3_points: 
       basketTeam1.threePointsThrow(); 
       break; 
      case R.id.team_2_3_points: 
       basketTeam2.threePointsThrow(); 
       break; 
      default: 
       break; 
     } 
    } 
} 

<Button 
android:layout_height="wrap_content" 
android:layout_width="120dp" 
android:text="+3 points" 
android:layout_gravity="center_horizontal" 
android:layout_marginTop="16dp" 
android:id="@+id/team_1_3_points" 
android:background="@color/colorAccent" 
android:textColor="@android:color/white" 
android:onClick="threePointsThrow"/> 
関連する問題