开发者

Android ListView crashes the application

I am trying to make a GUI in which I display 2 buttons on top followed by a list which increases as data is received. previously, I was appending data in TextView but then I switched to ListView but thats crashing my application.

Here is my code:

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;
       }
    }
}

My main.xml is:

<?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>

My list_item.xml is:

<?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>

Here is my 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开发者_如何学C-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_SUPERUSER" />
</manifest>

Here is my 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]  

I want that when start is clicked hello gets added to the list.

I cant figure out what am I doing wrong, Please help.


Your variable packets is never initialized, so that is likely the cause of your exception. You should at least declare it as an empty array with an initial capacity that makes sense for your application. For example:

String[] packets = new String[10];

Also, you need to change the id of the ListView in your layout to:

@android:id/list
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜