【问题标题】:Android ListView crashes the applicationAndroid ListView 使应用程序崩溃
【发布时间】:2011-08-28 22:40:36
【问题描述】:

我正在尝试制作一个 GUI,其中我在顶部显示 2 个按钮,然后是一个列表,该列表随着接收到数据而增加。 以前,我在 TextView 中附加数据,但后来我切换到 ListView 但那会导致我的应用程序崩溃。

这是我的代码:

package com.test;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
public class MainActivity extends ListActivity implements OnClickListener 
{
    String[] packets;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button start =(Button)findViewById(R.id.start);
        start.setOnClickListener(this);
        Button stop =(Button)findViewById(R.id.stop);
        stop.setOnClickListener(this);
        setListAdapter(new ArrayAdapter<String>(this,R.layout.list_item,packets));

    }
    public void onClick(View arg0) {
    switch(arg0.getId()){
    case R.id.start:
                         ((ArrayAdapter)getListAdapter()).add("hello");
            break;
       }
    }
}

我的 main.xml 是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    > 
    <Button android:id="@+id/start"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Start"
    />
    <Button android:id="@+id/stop"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Stop"
    />
   <ListView android:id="@+id/list" 
    android:layout_height="wrap_content"
    android:layout_width="fill_parent" 
    />
</LinearLayout>

我的 list_item.xml 是:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>

这是我的 AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.test"
      android:versionCode="1"
      android:versionName="1.0">


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
  <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_SUPERUSER" />
</manifest>

这是我的堆栈跟踪:

Thread [<1> main] (Suspended (exception RuntimeException))  
    ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1630    
    ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1646 
    ActivityThread.access$1500(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 121   
    ActivityThread$H.handleMessage(Message) line: 936   
    ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 123 
    ActivityThread.main(String[]) line: 3652    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 507  
    ZygoteInit$MethodAndArgsCaller.run() line: 862  
    ZygoteInit.main(String[]) line: 620 
    NativeStart.main(String[]) line: not available [native method]  

我希望在单击开始时将 hello 添加到列表中。

我不知道我做错了什么,请帮忙。

【问题讨论】:

  • 您能否发布此问题的堆栈跟踪和/或 logcat。
  • 我已经发了,请查收。

标签: android user-interface listview crash


【解决方案1】:

您的变量 packets 从未初始化,因此这可能是您的异常的原因。您至少应该将其声明为一个空数组,其初始容量对您的应用程序有意义。例如:

String[] packets = new String[10];

另外,您需要将布局中 ListView 的 id 更改为:

@android:id/list

【讨论】:

  • 这无济于事,应用程序仍然崩溃。
  • 我用我在您的代码中看到的另一个问题更新了我的答案。试试看。如果它不起作用,请在堆栈跟踪之外添加异常消息。另外,请更详细地描述您的应用程序何时崩溃。例如。当您单击按钮或在 Activity 显示之前会发生这种情况吗?
  • 它仍然无法正常工作,当应用程序启动时它崩溃了。 logcat 中没有显示错误/异常,但 eclipse 给出了上面的堆栈跟踪。我添加了 list_item.xml 。
  • 请发布您的 manifest.xml。另外,您是否尝试在 LogCat 窗口中查找异常?消息应该在那里。没有更多信息,很难提供帮助。我的猜测是,在您指定 MainActivity 的 manifest.xml 中可能有错字。
  • 是的,它确实试图找出它,但它什么也没显示。我添加了 AndroidManifest.xml
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-22
相关资源
最近更新 更多