简体   繁体   中英

My app crashed and I don't know why

I've tried to execute this code in my phone and in a AVD but unfortunately the app tp2 has stopped however the gradle build finished with no errors. Could someone help me with that please ?

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();

    }
}


}

It seems to be working but actually it crashes. The activity crashes and I can't find out why because nothing else (id) is displayed. Sorry for the bad english

Try cast field to EditText

field = (EditText)findViewById(R.id.field);

if there's still a problem please share the error message

setOnItemClickListener cannot be used with a Spinner. Use this setOnItemSelectedListener instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM