当我尝试动态地在ListView中添加项目时,按钮单击时崩溃

时间:2017-11-16 21:26:50

标签: java android listview

我有一个名为SELECT Day([Date]) AS [Day], Nz(Sum(AMT),0) AS [Day Total] FROM mytable WHERE Month([Date])=11 GROUP BY Day([Date]); 的{​​{1}},其中包含3 activityEfortActivityEditTextSpinner - saveButton。我的目标是在RadioGroup中动态添加数据(Button在名为ListView的另一个listView中实施。

问题在于,当我点击activity我的应用程序崩溃时。

以下是HistoryActivity中的代码:

saveButton

以下是EfortActivity的代码:

public class EfortMonitorizareActivity extends AppCompatActivity {

RadioGroup radioGroupDaNu;
Spinner spinnerTip;
EditText editTextDurata;
EditText editTextInainte;
EditText editTextDupa;
Intent intent;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_efort_monitorizare);
    intent = getIntent();   
    initializareComponente();    
}

private void initializareComponente() {
    radioGroupDaNu = (RadioGroup) findViewById(R.id.rg_activitateDANU);
    spinnerTip = (Spinner) findViewById(R.id.spin_tip_efort);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getApplicationContext(),
            R.array.TipEfort, R.layout.support_simple_spinner_dropdown_item);
    spinnerTip.setAdapter(adapter);
    editTextDurata = (EditText) findViewById(R.id.edt_durataAnt_efort);
    editTextInainte = (EditText) findViewById(R.id.edt_greuteInainteAnt_efort);
    editTextDupa = (EditText) findViewById(R.id.edt_greutateDupaAnt_efort);

    Button btn_inregistrareDate = (Button) findViewById(R.id.btn_adaugaDate_efort);
    btn_inregistrareDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (validare()) {
                Efort efort = createPlayerFromComponents();
                if (efort != null) {
                    intent.putExtra(Constants.ADD_EFORT_KEY,efort);
                    setResult(RESULT_OK,intent);
                    finish();
                }
            }
        }
    });
}

private Efort createPlayerFromComponents() {

    RadioButton checkDaNu = (RadioButton) findViewById(radioGroupDaNu.getCheckedRadioButtonId());
    String radioDaNu = checkDaNu.getText().toString();
    String tip = spinnerTip.getSelectedItem().toString();
    Integer durata = Integer.parseInt(editTextDurata.getText().toString());
    Integer inainte = Integer.parseInt(editTextInainte.getText().toString());
    Integer dupa = Integer.parseInt(editTextDupa.getText().toString());

    return new Efort(radioDaNu, tip, durata, inainte, dupa);
}

private boolean validare() {

    RadioButton nu = (RadioButton) findViewById(R.id.button_nu_efort);

    if (editTextDurata.getText() == null || editTextDurata.getText().toString().trim().isEmpty()) {
        Toast.makeText(getApplicationContext(), R.string.addplayer_number_error, Toast.LENGTH_SHORT).show();
        return false;
    }
    if (editTextInainte.getText() == null || editTextDurata.getText().toString().trim().isEmpty()) {
        Toast.makeText(getApplicationContext(), R.string.addplayer_number_error, Toast.LENGTH_SHORT).show();
        return false;
    }
    if (editTextDupa.getText() == null || editTextDurata.getText().toString().trim().isEmpty()) {
        Toast.makeText(getApplicationContext(), R.string.addplayer_number_error, Toast.LENGTH_SHORT).show();
        return false;
    }         }

班级HistoryActivity

public class IstoricActivity extends AppCompatActivity {

ListView lvEfort;
List<String> listaEfort = new ArrayList<>();
Button deconectare;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_istoric);

    deconectare=(Button)findViewById(R.id.btn_deconectareIstoric);
    deconectare.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
        }
    });
    //initializare componente
    lvEfort = (ListView) findViewById(R.id.lw_listaEfort);
    Efort efortDefault = new Efort("Da", "Tennis", 40, 55, 53);
    listaEfort.add(efortDefault.toString());

    //declarare + initializare adapter
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, listaEfort);
    lvEfort.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    }
}

1 个答案:

答案 0 :(得分:0)

当你打电话时,

  finish()

它将停止您的活动并完成它。请删除完成()