Добавить пользовательский ввод по кнопке

Я новичок в студии Android и действительно не знаю, что делаю. Я пытаюсь добавить элементы в список, но приложение вылетает, когда я пытаюсь перейти на вторую страницу. (в приложении две страницы, сбоев, когда вторая страница «пуста», не возникало.)

Ниже мой XML.

<ListView
    android:id = "@+id/list1"
    android:layout_width = "368dp"
    android:layout_height = "373dp"
    android:layout_marginBottom = "8dp"
    android:layout_marginEnd = "8dp"
    android:layout_marginLeft = "8dp"
    android:layout_marginRight = "8dp"
    android:layout_marginStart = "8dp"
    android:layout_marginTop = "8dp"
    app:layout_constraintBottom_toBottomOf = "parent"
    app:layout_constraintEnd_toEndOf = "parent"
    app:layout_constraintHorizontal_bias = "0.0"
    app:layout_constraintStart_toStartOf = "parent"
    app:layout_constraintTop_toTopOf = "parent"
    app:layout_constraintVertical_bias = "1.0" />

<EditText
    android:id = "@+id/laggtill"
    android:layout_width = "340dp"
    android:layout_height = "54dp"
    android:layout_marginBottom = "8dp"
    android:layout_marginEnd = "8dp"
    android:layout_marginLeft = "8dp"
    android:layout_marginRight = "8dp"
    android:layout_marginStart = "8dp"
    android:layout_marginTop = "8dp"
    android:hint = "@string/Sida2Hint"
    app:layout_constraintBottom_toTopOf = "@+id/list1"
    app:layout_constraintEnd_toEndOf = "parent"
    app:layout_constraintStart_toStartOf = "parent"
    app:layout_constraintTop_toTopOf = "parent"
    app:layout_constraintVertical_bias = "0.133" />

<Button
    android:id = "@+id/addButton"
    android:layout_width = "wrap_content"
    android:layout_height = "wrap_content"
    android:layout_marginBottom = "8dp"
    android:layout_marginEnd = "8dp"
    android:layout_marginLeft = "8dp"
    android:layout_marginRight = "8dp"
    android:layout_marginStart = "8dp"
    android:layout_marginTop = "8dp"
    android:text = "@string/Sida2Add"
    app:layout_constraintBottom_toBottomOf = "parent"
    app:layout_constraintEnd_toEndOf = "parent"
    app:layout_constraintHorizontal_bias = "0.942"
    app:layout_constraintStart_toStartOf = "parent"
    app:layout_constraintTop_toTopOf = "parent"
    app:layout_constraintVertical_bias = "0.143" />

А ниже мой класс java.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import java.util.ArrayList;

public class sida2 extends AppCompatActivity {

    private Button btnAdd;
    private EditText et;
    private ListView lv;

    ArrayList<String> Uppgifter = new ArrayList<String>();
    ArrayAdapter<String> adapter;

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

        btnAdd = (Button) findViewById(R.id.addButton);
        btnAdd.setOnClickListener((View.OnClickListener) this);
        et = (EditText) findViewById(R.id.laggtill);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, R.id.list1);

        lv = (ListView) findViewById(R.id.list1);
        lv.setAdapter(adapter);
    }

    public void onClick(View v) {
        String input = et.getText().toString();
        if (input.length() > 0) {
            // add string to the adapter, not the listview
            adapter.add(input);
            // no need to call adapter.notifyDataSetChanged(); as it is done by the adapter.add() method
        }
    }
}

Когда я пытаюсь перейти на «вторую страницу», приложение просто вылетает. В студии у меня нет ошибок, и я действительно не знаю, что делать. Благодарен за любую помощь!

0
0
37
1

Ответы 1

Открытый класс sida2 расширяет AppCompatActivity, реализует View.OnClickListener {

    private Button btnAdd;
    private EditText et;
    private ListView lv;

    ArrayList<String> Uppgifter = new ArrayList<String>();
    ArrayAdapter<String> adapter;

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

        btnAdd = (Button) findViewById(R.id.addButton);
        btnAdd.setOnClickListener(this);
        et = (EditText) findViewById(R.id.laggtill);
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, Uppgifter);

        lv = (ListView) findViewById(R.id.list1);
        lv.setAdapter(adapter);
    }

    @Override
    public void onClick(View view) {
        String input = et.getText().toString();
        if (input.length() > 0) {
            // add string to the adapter, not the listview
            Uppgifter.add(input);
            adapter.notifyDataSetChanged();

            // no need to call adapter.notifyDataSetChanged(); as it is done by the adapter.add() method
        }

    }
}

// xml

[![<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
    xmlns:app = "http://schemas.android.com/apk/res-auto"
    xmlns:tools = "http://schemas.android.com/tools"
    android:layout_width = "match_parent"
    android:layout_height = "match_parent"
    android:orientation = "vertical"
    >

    <ListView
        android:id = "@+id/list1"
        android:layout_width = "368dp"
        android:layout_height = "373dp"
        android:layout_marginBottom = "8dp"
        android:layout_marginEnd = "8dp"
        android:layout_marginLeft = "8dp"
        android:layout_marginRight = "8dp"
        android:layout_marginStart = "8dp"
        android:layout_marginTop = "8dp"
        app:layout_constraintBottom_toBottomOf = "parent"
        app:layout_constraintEnd_toEndOf = "parent"
        app:layout_constraintHorizontal_bias = "0.0"
        app:layout_constraintStart_toStartOf = "parent"
        app:layout_constraintTop_toTopOf = "parent"
        app:layout_constraintVertical_bias = "1.0" />

    <EditText
        android:id = "@+id/laggtill"
        android:layout_width = "340dp"
        android:layout_height = "54dp"
        android:layout_marginBottom = "8dp"
        android:layout_marginEnd = "8dp"
        android:layout_marginLeft = "8dp"
        android:layout_marginRight = "8dp"
        android:layout_marginStart = "8dp"
        android:layout_marginTop = "8dp"
        android:hint = "aaaa"
        app:layout_constraintBottom_toTopOf = "@+id/list1"
        app:layout_constraintEnd_toEndOf = "parent"
        app:layout_constraintStart_toStartOf = "parent"
        app:layout_constraintTop_toTopOf = "parent"
        app:layout_constraintVertical_bias = "0.133" />

    <Button
        android:id = "@+id/addButton"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_marginBottom = "8dp"
        android:layout_marginEnd = "8dp"
        android:layout_marginLeft = "8dp"
        android:layout_marginRight = "8dp"
        android:layout_marginStart = "8dp"
        android:layout_marginTop = "8dp"
        android:text = "aaa"
        app:layout_constraintBottom_toBottomOf = "parent"
        app:layout_constraintEnd_toEndOf = "parent"
        app:layout_constraintHorizontal_bias = "0.942"
        app:layout_constraintStart_toStartOf = "parent"
        app:layout_constraintTop_toTopOf = "parent"
        app:layout_constraintVertical_bias = "0.143" />

</LinearLayout>][1]][1]

PS: Попробуйте отладить свое приложение .. Вы многому научитесь. :)

Спасибо за ответ, приложение больше не вылетает, а просто закрывается. :(

Mr.Jones 29.05.2018 15:28

Нет, извините, у меня все еще не работает. Может быть, предыдущая страница так сказать мешает следующей? ibb.co/i21tHd

Mr.Jones 29.05.2018 15:42

Другие вопросы по теме