开发者

Class level spinner object unresolvable from method in same class in Android

Trying my hand at java and android and have run into a problem which I cannot determine is java related, or android related. Essentially I have created some spinners with AdapterArrays on the main onCreate method of the activity, the indices of which I would like to reference from a method I've created which is called from a button using the android:onclick xml attribute.

However, the onclick method cannot resolve the spinner object I am referencing. I imagine I am improperly placing the code somewhere within the app, and since I'm very new to java I'm not sure if I开发者_运维技巧'm supposed to be overriding the method, and what I'm doing the the View parameter which the android documentation says I am required to pass. Psedo XML as follows:

<Button
 android:onclick="method" />

Pseduo app.java as follows:

public class sampleApp extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
Spinner cloudSpinner = (Spinner) findViewById(R.id.cloud_spinner);
ArrayAdapter<CharSequence> cloudAdapter = ArrayAdapter.createFromResource(this, R.array.cloud_array, android.R.layout.simple_spinner_item);
cloudAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cloudSpinner.setAdapter(cloudAdapter);
}

public void method(View v){
if(cloudSpinner.getSelectedItem()){
}

}
}

From there the spinner object is unresolvable. Can anyone point out where I'm going wrong?


Spinner cloudSpinner is declared inside onCreate().Move it outside.That will solve 'spinner object is unresolvable' problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜