在一个活动和布局中使用少量ListView

时间:2013-11-07 17:55:45

标签: android listview crash android-activity listactivity

我正在尝试组织指定ListView中的SMS消息收到的数据。 我尝试创建和活动,在一个布局中包含3个ListView。 但是,在运行活动时它会崩溃。 有人可以帮忙吗?

以下是XML代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"

android:orientation="horizontal" >

<ListView
    android:id="@+id/idList"
    android:layout_width="112dp"
    android:layout_height="384dp"
    android:layout_gravity="center" >
</ListView>

<ListView
    android:id="@+id/namesList"
    android:layout_width="96dp"
    android:layout_height="387dp"
    android:layout_gravity="center" >

</ListView>

<ListView
    android:id="@+id/phonesList"
    android:layout_width="wrap_content"
    android:layout_height="382dp"
    android:layout_gravity="center" >

</ListView>

</LinearLayout>

这是活动代码:

import java.util.ArrayList;

  import android.app.Activity;
  import android.app.ListActivity;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.os.Bundle;
  import android.telephony.SmsMessage;
  import android.view.Menu;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;

  public class DataLists extends ListActivity implements OnClickListener {
  ListView idList, namesList, phonesList;
  MyReciever mr;
  ArrayList<String>ids= new ArrayList<String>();
  ArrayList<String>names=new ArrayList<String>();
  ArrayList<String>phones=new ArrayList<String>();

  ArrayAdapter<String> idAdapter, namesAdapter, phonesAdapter;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    return super.onCreateOptionsMenu(menu);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.details_lists);
    idList=(ListView)findViewById(R.id.idList);
    namesList=(ListView)findViewById(R.id.namesList);
    phonesList=(ListView)findViewById(R.id.phonesList);
    idAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,ids );
    idList.setAdapter(idAdapter);
    namesAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, names);
    namesList.setAdapter(namesAdapter);
    phonesAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, phones);
    phonesList.setAdapter(phonesAdapter);

}
public void addItemToIdList(String st)
{
    ids.add(st);
    idAdapter.notifyDataSetChanged();
}

public void addItemToNamesList(String st)
{
    names.add(st);
    namesAdapter.notifyDataSetChanged();
}

public void addItemToPhonesList(String st)
{
    phones.add(st);
    phonesAdapter.notifyDataSetChanged();
}


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}

private class MyReciever extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Bundle bundle=intent.getExtras();
        SmsMessage[]msgs=null;
        if(bundle!=null)
        {
            Object[]pdus=(Object[]) bundle.get("pdus");
            msgs=new SmsMessage[pdus.length];
            for(int i=0;i<msgs.length;i++)
            {
                int index=0, prev=0;
                String msgBody=msgs[i].getMessageBody().toString();
                 index=msgBody.indexOf(';');
                 prev=index;
                 String name=msgBody.substring(0, index);
                 addItemToNamesList(name);
                 msgBody=msgBody.substring(index+1);
                 index=msgBody.indexOf(';');
                 String id=msgBody.substring(prev, index);
                 addItemToIdList(id);
                 msgBody=msgBody.substring(index+1);
                 String phone=msgBody;
                 addItemToPhonesList(phone);

            }
        }
    }

}

}

1 个答案:

答案 0 :(得分:0)

您已延长ListActivity。这是一个便利类,可用于显示单个ListView。崩溃在logcat / stacktrace中清楚地描述:

java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

由于您的布局包含3个ListView,因此您可能无法使用ListActivity。只需更改您的活动以扩展Activity并从那里开始。