Android ListView使应用程序崩溃
我正在尝试制作一个GUI,其中我在顶部显示两个按钮,然后是一个列表,随着数据的接收而增加。 以前,我在TextView中添加数据,但后来我切换到ListView,但这会使我的应用程序崩溃 这是我的密码:Android ListView使应用程序崩溃,android,user-interface,listview,crash,Android,User Interface,Listview,Crash,我正在尝试制作一个GUI,其中我在顶部显示两个按钮,然后是一个列表,随着数据的接收而增加。 以前,我在TextView中添加数据,但后来我切换到ListView,但这会使我的应用程序崩溃 这是我的密码: package com.test; import android.app.Activity; import android.app.ListActivity; import android.content.Context; import android.os.Bundle; import an
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>
这是我的stacktrace:
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被添加到列表中
我不知道我做错了什么,请帮助。您的变量数据包从未初始化,因此这可能是导致异常的原因。您至少应该将其声明为一个空数组,其初始容量对您的应用程序有意义。例如:
String[] packets = new String[10];
此外,您需要将布局中ListView的id更改为:
@android:id/list
你能发布这个问题的堆栈跟踪和/或日志吗?我已经发布了,请检查。这无助于解决问题,应用程序仍然崩溃。我用代码中的另一个问题更新了我的答案。试试看。如果不起作用,请在堆栈跟踪之外添加异常消息。另外,请更详细地描述应用程序崩溃时的情况。例如它是在您单击按钮时发生的还是在活动显示之前发生的?它仍然不工作,在应用程序启动时崩溃。logcat中没有显示错误/异常,但eclipse给出了上述堆栈跟踪。我已经添加了list_item.xml。请发布您的manifest.xml。另外,您是否尝试在LogCat窗口中查找异常?信息应该在那里。没有更多的信息,很难帮助你。我猜您的manifest.xml中可能有输入错误,您在其中指定了MainActivity。是的,它确实试图找到它,但没有显示任何内容。我已经添加了AndroidManifest.xml