You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中含ListView的页面跳转崩溃问题求助

Fix for Your Android Studio ListView Crash Issue

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 (Uppgifter ArrayList), not the ID of your ListView.
  • android.R.layout.simple_expandable_list_item_1 is designed for ExpandableListView, not regular ListView—use android.R.layout.simple_list_item_1 instead 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 ...
}

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.xml file has the Sida2Hint and Sida2Add string 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:57:17