2017-04-14 28 views
1

Androidスタジオに問題があり、作成しているランダマイザアプリケーションでsetOnClickListenerを解決できないようです。以下はアプリケーションのコードです。私は問題を特定できないようだが、どんな助けでも大いに感謝するだろう。AndroidスタジオがsetOnClickListenerを解決できない

public class MainActivity extends AppCompatActivity { 

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

TextView textOne = (TextView) findViewById(R.id.textView); 
TextView textTwo = (TextView) findViewById(R.id.textView2); 
TextView textThree = (TextView) findViewById(R.id.textView3); 
TextView textFour = (TextView) findViewById(R.id.textView4); 
TextView textFive = (TextView) findViewById(R.id.textView5); 
TextView textSix = (TextView) findViewById(R.id.textView6); 
TextView textSeven = (TextView) findViewById(R.id.textView7); 
TextView textEight = (TextView) findViewById(R.id.textView8); 
Button button = (Button) findViewById(R.id.button); 

String[] myStamina = {"Stamina", "300%"}; 
String[] mySize = {"Off", "Mega", "Mini"}; 
String[] myHead = {"Off", "Flower", "Bunny"}; 
String[] myBody = {"Off", "Metal", "Clear", "Tail", "Rocket belt", "Screw", "Back shield"}; 
String[] myStatus = {"Off", "Curry", "Reflect"}; 
String[] myGravity = {"Off", "Light", "Heavy"}; 
String[] mySpeed = {"Off", "Fast", "Slow"}; 
String[] myCamera = {"Off", "Fixed", "Angled"}; 

int random1 = (int) ((Math.random() * 1)); 
int random2 = (int) ((Math.random() * 2)); 
int random3 = (int) ((Math.random() * 6)); 

button.setOnClickListener(new View.OnClickListener(){ 

    public void onClick(View v){ 
     textOne.setText(myStamina[random1]); 
     textTwo.setText(mySize[random2]); 
     textThree.setText(myHead[random2]); 
     textFour.setText(myBody[random3]); 
     textFive.setText(myStatus[random2]); 
     textSix.setText(myGravity[random2]); 
     textSeven.setText(mySpeed[random2]); 
     textEight.setText(myCamera[random2]); 
    } 



}); 

}

+0

あなたはメソッドの外にたくさんのコードを入れました...小さなセクションでアプリケーションをテストしてみてください –

答えて

1
  1. globalとしてあなたTextViewButtonウィジェットを宣言します。
  2. 設定ボタンonClickリスナーOnCreate()メソッドから。

これを試してみてください:

public class MainActivity extends AppCompatActivity { 

    TextView textOne, textTwo, textThree, textFour, textFive, textSix, textSeven, textEight; 
    Button button; 

    String[] myStamina = {"Stamina", "300%"}; 
    String[] mySize = {"Off", "Mega", "Mini"}; 
    String[] myHead = {"Off", "Flower", "Bunny"}; 
    String[] myBody = {"Off", "Metal", "Clear", "Tail", "Rocket belt", "Screw", "Back shield"}; 
    String[] myStatus = {"Off", "Curry", "Reflect"}; 
    String[] myGravity = {"Off", "Light", "Heavy"}; 
    String[] mySpeed = {"Off", "Fast", "Slow"}; 
    String[] myCamera = {"Off", "Fixed", "Angled"}; 

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

    textOne = (TextView) findViewById(R.id.textView); 
    textTwo = (TextView) findViewById(R.id.textView2); 
    textThree = (TextView) findViewById(R.id.textView3); 
    textFour = (TextView) findViewById(R.id.textView4); 
    textFive = (TextView) findViewById(R.id.textView5); 
    textSix = (TextView) findViewById(R.id.textView6); 
    textSeven = (TextView) findViewById(R.id.textView7); 
    textEight = (TextView) findViewById(R.id.textView8); 
    button = (Button) findViewById(R.id.button); 

    int random1 = (int) ((Math.random() * 1)); 
    int random2 = (int) ((Math.random() * 2)); 
    int random3 = (int) ((Math.random() * 6)); 

    button.setOnClickListener(new View.OnClickListener(){ 

     public void onClick(View v){ 
      textOne.setText(myStamina[random1]); 
      textTwo.setText(mySize[random2]); 
      textThree.setText(myHead[random2]); 
      textFour.setText(myBody[random3]); 
      textFive.setText(myStatus[random2]); 
      textSix.setText(myGravity[random2]); 
      textSeven.setText(mySpeed[random2]); 
      textEight.setText(myCamera[random2]); 
     } 

    }); 

} 
2
このよう onCreate()方法にコードを移動

public class MainActivity extends AppCompatActivity { 

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

    TextView textOne = (TextView) findViewById(R.id.textView); 
    TextView textTwo = (TextView) findViewById(R.id.textView2); 
    TextView textThree = (TextView) findViewById(R.id.textView3); 
    TextView textFour = (TextView) findViewById(R.id.textView4); 
    TextView textFive = (TextView) findViewById(R.id.textView5); 
    TextView textSix = (TextView) findViewById(R.id.textView6); 
    TextView textSeven = (TextView) findViewById(R.id.textView7); 
    TextView textEight = (TextView) findViewById(R.id.textView8); 
    Button button = (Button) findViewById(R.id.button); 

    String[] myStamina = {"Stamina", "300%"}; 
    String[] mySize = {"Off", "Mega", "Mini"}; 
    String[] myHead = {"Off", "Flower", "Bunny"}; 
    String[] myBody = {"Off", "Metal", "Clear", "Tail", "Rocket belt", "Screw", "Back shield"}; 
    String[] myStatus = {"Off", "Curry", "Reflect"}; 
    String[] myGravity = {"Off", "Light", "Heavy"}; 
    String[] mySpeed = {"Off", "Fast", "Slow"}; 
    String[] myCamera = {"Off", "Fixed", "Angled"}; 

    int random1 = (int) ((Math.random() * 1)); 
    int random2 = (int) ((Math.random() * 2)); 
    int random3 = (int) ((Math.random() * 6)); 

    button.setOnClickListener(new View.OnClickListener(){ 

     public void onClick(View v){ 
      textOne.setText(myStamina[random1]); 
      textTwo.setText(mySize[random2]); 
      textThree.setText(myHead[random2]); 
      textFour.setText(myBody[random3]); 
      textFive.setText(myStatus[random2]); 
      textSix.setText(myGravity[random2]); 
      textSeven.setText(mySpeed[random2]); 
      textEight.setText(myCamera[random2]); 
     } 

    }); 


} 
+0

TextViewsと配列は最終的になる必要があります –

+0

TextViewと配列はfinalとして宣言する必要があります。 – FAT

0
()メソッドOnCreateの内部コードを移動

0

溶液を、あなたに提示された、今いくつかの理論を詳しく調べてみましょう: をあなたはアンドロイド関連として、あなたの質問にタグを付けているが、私が見てあなたを示唆してよいくつかで少しobject oriented programming principles。クラス内で実行可能なコード(オブジェクトメンバーなどの他のメソッドを呼び出すコード)を使用する場合は、メソッド内に配置する必要があります。アイデアは、オブジェクトは、そのフィールドは、すべてのコード外方法がwhen the class is loaded (static code blocks)またはwhen an object of that class type is created(コンストラクタ)のいずれかを実行する

注インスタンス化される場合心配することなく、すべてのフィールドを使用するためにcreated(instantiated)最初でなければならないということです。

戻ってあなたの元の質問に行く:あなたはそのクリックリスナーを設定しようとすると、アンドロイドの仮想マシンでもあるため、システムによって作成されたMainActivityのインスタンスである実行中のその時点で、「ボタン」は、現時点では誰であるか伝えることができません完全には作成されません(インスタンス化されます)。

関連する問題