【问题标题】:Using few ListView's in one activity and layout在一个活动和布局中使用几个 ListView
【发布时间】:2013-11-07 17:55:45
【问题描述】:

我正在尝试在指定的 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);

            }
        }
    }

}

}

【问题讨论】:

  • 将它们放在ViewPager 中……Google Play 应用就是一个很好的例子。
  • 感谢您的快速响应,但我想一次显示所有 3 个 ListView。 Eclipse 没有显示任何错误,但在运行时它崩溃了。我到底错在哪里?我该怎么做我上面说的?
  • 在这里发布你的日志
  • 11-07 18:39:23.736: E/AndroidRuntime(1257): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.avissmsapplication/com.example.avissmsapplication.DataLists} :java.lang.RuntimeException:您的内容必须有一个 ID 属性为 'android.R.id.list' 的 ListView 11-07 18:39:23.736: E/AndroidRuntime(1257): at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2180) 11-07 18:39:23.736: E/AndroidRuntime(1257): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)

标签: android listview crash android-activity listactivity


【解决方案1】:

您已扩展ListActivity。这是一个方便类,可用于显示单个ListView。您的 logcat/stacktrace 中清楚地描述了崩溃:

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

由于您的布局包含 3 个ListViews,您可能无论如何都不能使用ListActivity。只需将您的活动更改为扩展 Activity 并从那里开始。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-04
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-06
    • 2012-09-13
    相关资源
    最近更新 更多