我已尝试在手机和AVD中执行此代码但不幸的是,应用程序tp2已停止,但是gradle构建完成且没有错误。 有人可以帮帮我吗?
package com.example.aboukora.tp2;
public class MainActivity extends Activity {
Spinner spinner;
EditText field;
Button addBtn,UpdateBtn,clearBtn;
ArrayAdapter<String> adapter;
ArrayList<String> films = new ArrayList<String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner= (Spinner) findViewById(R.id.spinner1);
field = findViewById(R.id.field);
addBtn= (Button) findViewById(R.id.addbtn);
UpdateBtn= (Button) findViewById(R.id.updatebtn);
clearBtn= (Button) findViewById(R.id.clearbtn);
//ADAPTER
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_single_choice,films);
spinner.setAdapter(adapter);
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
field.setText(films.get(i));
}
});
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
add();
}
});clearBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
delete();
}
});
UpdateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
update();
}
});
}
//methods
private void add(){
String film=field.getText().toString();
if(!film.isEmpty() && film.length() >0){
adapter.add(film);
adapter.notifyDataSetChanged();
field.setText("");
Toast.makeText(getApplicationContext()," Ajoute" + film, Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(), "Rien a ajouter", Toast.LENGTH_SHORT).show();
}
}
private void update() {
String film = field.getText().toString();
int pos = spinner.getSelectedItemPosition();
if (!film.isEmpty() && film.length() > 0) {
adapter.remove(films.get(pos));
adapter.insert(film,pos);
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext()," Modifie" + film, Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext()," Pas Modifie" + film, Toast.LENGTH_SHORT).show();
}
}
private void delete(){
int pos = spinner.getSelectedItemPosition();
if(pos > -1){
adapter.remove(films.get(pos));
adapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext()," Supprime" , Toast.LENGTH_SHORT).show();
field.setText("");
}
else{
Toast.makeText(getApplicationContext()," Rien a supprimer" , Toast.LENGTH_SHORT).show();
}
}
}
它似乎工作但实际上它崩溃了。活动崩溃,我无法找到原因,因为没有其他(id)显示。抱歉英文不好
答案 0 :(得分:0)
尝试将字段转换为EditText
constructor(public dialog: MatDialog, private webApi: WebapiService) {
}
如果仍有问题,请分享错误消息
答案 1 :(得分:0)
setOnItemClickListener
不能与Spinner一起使用。请改用此setOnItemSelectedListener
。