2017-04-14 118 views
1

在使用隨機工具應用程序進行Android Studio時出現問題,我似乎無法解析setOnClickListener。下面是應用程序的代碼,我似乎無法確定任何幫助將不勝感激的問題。Android studio無法解析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. 聲明你TextViewButton部件爲global
  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和Arrays應該聲明爲final。 – FAT

0

移動內部onCreate()方法的代碼。

0

溶液呈現給大家,現在讓我們深入到一些理論: 雖然您已爲問題由於Android相關的,我會建議你看看一些object oriented programming principles。如果你想在你的類中擁有可執行代碼(即在對象成員等上調用其他方法的代碼),你應該把它放在方法中。這個想法是,一個對象必須是爲了使用所有的字段而不如果字段被實例化

注意,所有的代碼以外的方法將被執行或者when the class is loaded (static code blocks)when an object of that class type is created(構造)擔心第一created(instantiated)

回到你原來的問題:當你嘗試設置它的點擊監聽器時,android虛擬機甚至不能告訴誰是「按鈕」,因爲在執行時,系統創建的MainActivity實例是不完全創建(實例化)。

相關問題