我是android studio的新手,并不知道我在做什么。我正在尝试将项目添加到列表视图中,但当我尝试转到第二页时,应用程序崩溃了。 (该应用有两页,当第二页“空”时没有发生崩溃。)
以下是我的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)
public class sida2 extends 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:尝试调试你的应用程序..你会学到很多东西。 :)