Android Studio中含ListView的页面跳转崩溃问题求助
Hey there! Let's get your app working properly—your crash is caused by two key issues in your Java code. Let's break them down and fix them step by step:
1. Incorrect ArrayAdapter Constructor Parameters
You made a mistake when initializing the ArrayAdapter:
- The third parameter should be your data source (
UppgifterArrayList), not the ID of your ListView. android.R.layout.simple_expandable_list_item_1is designed for ExpandableListView, not regular ListView—useandroid.R.layout.simple_list_item_1instead for standard list items.
Wrong Code:
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, R.id.list1);
Fixed Code:
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Uppgifter);
2. Missing View.OnClickListener Implementation
You tried to set the click listener with btnAdd.setOnClickListener((View.OnClickListener) this);, but your sida2 class doesn't implement the View.OnClickListener interface. This causes a ClassCastException that crashes your app.
There are two easy fixes for this:
Option 1: Implement the Interface
Update your class definition to implement the interface, and keep your existing onClick method:
public class sida2 extends AppCompatActivity implements View.OnClickListener { // ... your existing code ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sida2); btnAdd = findViewById(R.id.addButton); btnAdd.setOnClickListener(this); // No need for casting now et = findViewById(R.id.laggtill); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Uppgifter); lv = findViewById(R.id.list1); lv.setAdapter(adapter); } // ... your existing onClick method ... }
Option 2: Use an Anonymous Inner Class (Recommended)
This avoids modifying the class definition and keeps the click logic self-contained:
Replace the line btnAdd.setOnClickListener((View.OnClickListener) this); with:
btnAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String input = et.getText().toString(); if (!input.isEmpty()) { Uppgifter.add(input); adapter.notifyDataSetChanged(); // Alternatively, you can use adapter.add(input) which auto-notifies, but adding to the list first is clearer et.setText(""); // Optional: Clear the EditText after adding } } });
Full Fixed Java Code
Here's the complete corrected activity code using the anonymous inner class approach:
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 = findViewById(R.id.addButton); et = findViewById(R.id.laggtill); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Uppgifter); lv = findViewById(R.id.list1); lv.setAdapter(adapter); // Set click listener with anonymous inner class btnAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String input = et.getText().toString(); if (!input.isEmpty()) { Uppgifter.add(input); adapter.notifyDataSetChanged(); et.setText(""); // Clear input field after adding } } }); } }
Quick Notes
- Make sure your
strings.xmlfile has theSida2HintandSida2Addstring resources defined (this won't cause a crash, but will throw a build error if missing). - I added
et.setText("")to clear the EditText after adding an item—this is a nice UX touch, feel free to remove it if you don't want it.
内容的提问来源于stack exchange,提问作者Mr.Jones

