gpt4 book ai didi

java - Android Spinners - 无法解析符号 - setOnItemSelectedListener

转载 作者:行者123 更新时间:2023-12-02 03:11:15 24 4
gpt4 key购买 nike

我是 Android 编程新手,正在尝试实现微调器,但我收到 setOnItemSelectedListener、setDropDownViewResource 和 setAdapter 的“无法解析符号”错误。我很困惑。我尝试添加更多导入,但他们没有做任何事情。

这是我的代码:

public class MainActivity extends AppCompatActivity implements OnItemSelectedListener {
public final static String EXTRA_MESSAGE = "com.example.FinalProject.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);

// Create an ArrayAdapter using the string array and a default spinner layout

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.verb_endings_array, android.R.layout.simple_spinner_item);

// Specify the layout to use when the list of choices appears

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// Apply the adapter to the spinner

spinner.setAdapter(adapter);

public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}

public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}

/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}

最佳答案

移动这个

Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);

onCreate方法内,但是在外部声明微调器,(您肯定希望稍后使用它......:))

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

spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
}

关于java - Android Spinners - 无法解析符号 - setOnItemSelectedListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41004063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com