2017-05-25 227 views
0

我在Android Studio中實現微調。我有下面的代碼。我收到錯誤MainActivity must either be declared abstract or implement abstract method onItemSelected(AdapterView<?>, View, int, long) in OnItemSelectedListener。但正如你在下面看到的,我已經實施了onItemSelected(AdapterView<?>, View, int, long)類必須聲明爲抽象或實現onItemSelected在OnItemSelectedListener中的抽象方法

此外,當我運行的代碼,我看到錯誤MainActivity is not abstract and does not override abstract method onNothingSelected(AdapterView<?>) in OnItemSelectedListener

我缺少什麼?

public class MainActivity extends AppCompatActivity implements OnItemSelectedListener{ 

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     final Context context = getApplicationContext(); 

setContentView(R.layout.activity_main); 

     Spinner spinner = (Spinner) findViewById(R.id.spinner_supplychainEntities); 

     ArrayAdapter adapter = ArrayAdapter.createFromResource(this, 
       R.array.spinner_options, android.R.layout.simple_spinner_item); 

     spinner.setOnItemSelectedListener(MainActivity.this); 
     spinner.setAdapter(adapter); 

} 

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
       //stuff here to handle item selection 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 

       Log.i("GTOUTOUT", "Nothing Selected"); 
      } 
     }); 
} 

回答

1

你應該選擇只有一個辦法來實現OnItemSelectedListener,其實你已經兩個OnItemSelectedListener的方式來實現,而不是覆蓋onItemSelected和onNothingSelected方法

// 要麼刪除此

spinner.setOnItemSelectedListener(新AdapterView.OnItemSelectedListener(){ @Override 公共無效onItemSelected(適配器查看父,觀景,INT位置,長的id){// 的東西在這裏處理項目選擇 }

 @Override 
     public void onNothingSelected(AdapterView<?> parent) { 

      Log.i("GTOUTOUT", "Nothing Selected"); 
     } 
    }); 

OR刪除此

工具OnItemSelectedListener //從類聲明 微調。 setOnItemSelectedListener(MainActivity.this);

1

你正在做兩次,你必須保持一個。此外,移動你的代碼的onCreate方法中:

public class MainActivity extends AppCompatActivity { 

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     final Context context = getApplicationContext(); 

     setContentView(R.layout.activity_main); 

     Spinner spinner = (Spinner) findViewById(R.id.spinner_supplychainEntities); 

     ArrayAdapter adapter = ArrayAdapter.createFromResource(this, 
       R.array.spinner_options, android.R.layout.simple_spinner_item); 

     spinner.setAdapter(adapter); 
     spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
       //stuff here to handle item selection 
      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 

       Log.i("GTOUTOUT", "Nothing Selected"); 
      } 
     }); 
    } 
    } 
0

您需要實現在OnItemSelectedListener定義的抽象函數能夠實例化的活動類。試試這個

public class MainActivity extends AppCompatActivity implements OnItemSelectedListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     final Context context = getApplicationContext(); 

     setContentView(R.layout.activity_main); 

     Spinner spinner = (Spinner) findViewById(R.id.spinner_supplychainEntities); 

     ArrayAdapter adapter = ArrayAdapter.createFromResource(this, 
      R.array.spinner_options, android.R.layout.simple_spinner_item); 

     spinner.setOnItemSelectedListener(this); 
     spinner.setAdapter(adapter); 

    } 

    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
     //stuff here to handle item selection 
    } 

    @Override 
    public void onNothingSelected(AdapterView<?> parent) { 

     Log.i("GTOUTOUT", "Nothing Selected"); 
    } 
} 
相關問題