سؤال

Hi I'm completely new to android programming and use AIDE via tablet.

I'm trying to create a very basic program with a Spinner box that gives output on the selection Ive made via an TextView or System.Out.printIn. (Perhaps the next step up from Hello world - if you will)

For some reason that I cannot fathom,the compiler refuses to recognise the OnClickListener and gives the error message 'Unknown method OnClickListener in Android.Widget.Spinner' When I have already checked this in the imports.

As a matter of interest I have changed the name of the Spinner and the error seems to dissapear, the problem then is the Spinner name. I have tried several variations on this, and have came to the conclusion that the best option for me is to create a variable just after Main Acivity, and before the layout is declared. 

I have also disabled one of the overrides in order to resolve my problem

has anyone got an idea what the problem could be?

package com.BGilbert.AUC;

import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.View.OnClickListener;
import android.widget.Spinner.*;
import android.view.*;


public class MainActivity extends Activity {;

  String Fbstring;
  OnClickListener Myonclick;

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main);
    final Spinner Fbspinner=(Spinner)findViewById(R.id.Spinner);

    // The problem is with this line. OnClickListener just wont be        
    // recognised
    Fbspinner.OnClickListener(Myonclick);
  }

  // Override previously disabled
  @Override
  public void Onselect(AdapterView<?> parent,View V, int pos, long id) {
    Fbstring = parent.getItemAtPosition(pos).toString();
    System.out.println(Fbstring);
  }

  @Override
  public void onNothingSelected(AdapterView<?> arg0) {
  }
}
هل كانت مفيدة؟

المحلول

You can't set an onClickListener on a spinner, only on it's views which is too advanced for you at the moment. Instead, use an onItemSelectedListener.

public class MainActivity extends Activity extends Activity implements OnItemSelectedListener {

public void onItemSelected(AdapterView<?> parent, View view, 
        int pos, long id) {
     ...
     ...
}

You should read the documentation first http://developer.android.com/guide/topics/ui/controls/spinner.html

Also try to use standard naming conventions:

http://www.oracle.com/technetwork/java/codeconv-138413.html http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367

Finally, you have many problems in this code, e.g.

public class MainActivity extends Activity {;

Note the semicolon at the end.

Get your code compiling first, then come back with your next question.

Good luck

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top