android - ListActivity 在启动时崩溃

标签 android listview crash

我有一个基本的 ListActivity 和一些假测试数据。当我运行该应用程序时,它立即崩溃。我尝试调试但没有得到任何反馈。它在说开始后就停止了: Intent 。

public class PersonList extends ListActivity
{
private ArrayList<Person> persons;
private TwoLineListAdapter personAdapter;

@Override
protected void onCreate (Bundle savedInstanceState)
{
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_person_list);
   getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

   persons =  getPersonsForListView();
   personAdapter = new TwoLineListAdapter(persons);
   setListAdapter(personAdapter);
}

@Override
public boolean onCreateOptionsMenu (Menu menu)
{
   getMenuInflater().inflate(R.menu.menu_person_list, menu);
   return true;
}

@Override
public boolean onOptionsItemSelected (MenuItem item)
{
    int id = item.getItemId();

   if (id == R.id.action_settings)
   {
       return true;
   }

   return super.onOptionsItemSelected(item);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id)
{
   Person p = personAdapter.getItem(position);
   Toast.makeText(this, p.getName(), Toast.LENGTH_LONG).show();
}

/**
 * Adapter class
 * Makes 2 line list items
 */
private class TwoLineListAdapter extends BaseAdapter
{
   ArrayList<Person> pers;

   public TwoLineListAdapter(ArrayList<Person> p)
   {
      pers= p;
   }

   public int getCount()
   {
       if (pers != null)
           return pers.size();

       return 0;
   }

   public Person getItem(int pos)
   {
       return pers.get(pos);
   }

   public long getItemId(int position)
   {
       return position;
   }

   public View getView(int position, View convertView, ViewGroup parent)
   {
       LayoutInflater inflater = getLayoutInflater();
       View row;
       row = inflater.inflate(R.layout.list_item, parent, false);
       TextView name, info;
       name = (TextView) row.findViewById(R.id.txt1);
       info = (TextView) row.findViewById(R.id.txt2);
       name.setText(pers.get(position).getName());
       info .setText(pers.get(position).getInfo());

       return (row);
    }
}

public ArrayList<Person> getPersonsForListView()
{
   ArrayList<Person> personList = new ArrayList<Person>();

   for(int i = 0; i < 10; i++)
   {
       Person p = new Person("Name " + i, “Info ” + i);
       personList.add(p);
   }

   return personList;
}
}

这是列表:

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

编辑 - 我之前没有任何反馈,但我再次运行它,我得到了这个:

05-21 22:17:43.437    6642-6642/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.apptech.app, PID: 6642
android.content.res.Resources$NotFoundException: String resource ID #0x0
        at android.content.res.Resources.getText(Resources.java:299)
        at android.widget.TextView.setText(TextView.java:4132)
        at com.apptech.app.PersonList$TwoLineListAdapter.getView(PersonList.java:119)
        at android.widget.AbsListView.obtainView(AbsListView.java:2347)
        at android.widget.ListView.makeAndAddView(ListView.java:1864)
        at android.widget.ListView.fillDown(ListView.java:698)
        at android.widget.ListView.fillFromTop(ListView.java:759)
        at android.widget.ListView.layoutChildren(ListView.java:1673)
        at android.widget.AbsListView.onLayout(AbsListView.java:2151)
        at android.view.View.layout(View.java:15671)
        at android.view.ViewGroup.layout(ViewGroup.java:5038)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
        at android.view.View.layout(View.java:15671)
        at android.view.ViewGroup.layout(ViewGroup.java:5038)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
        at android.view.View.layout(View.java:15671)
        at android.view.ViewGroup.layout(ViewGroup.java:5038)
        at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:494)
        at android.view.View.layout(View.java:15671)
        at android.view.ViewGroup.layout(ViewGroup.java:5038)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
        at android.view.View.layout(View.java:15671)
        at android.view.ViewGroup.layout(ViewGroup.java:5038)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2086)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
        at android.view.Choreographer.doCallbacks(Choreographer.java:580)
        at android.view.Choreographer.doFrame(Choreographer.java:550)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Edit2 - 我清理了该项目,现在它可以工作了

最佳答案

当我从您的项目创建演示时,如下所示:

public class MainActivity extends ListActivity {
private ArrayList<Person> persons;
private TwoLineListAdapter personAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

    persons = getPersonsForListView();
    personAdapter = new TwoLineListAdapter(persons);
    setListAdapter(personAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // getMenuInflater().inflate(R.menu.menu_person_list, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    Person p = personAdapter.getItem(position);
    Toast.makeText(this, p.getName(), Toast.LENGTH_LONG).show();
}

/**
 * Adapter class Makes 2 line list items
 */
private class TwoLineListAdapter extends BaseAdapter {
    ArrayList<Person> pers;

    public TwoLineListAdapter(ArrayList<Person> p) {
        pers = p;
    }

    public int getCount() {
        if (pers != null)
            return pers.size();

        return 0;
    }

    public Person getItem(int pos) {
        return pers.get(pos);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        View row;
        row = inflater.inflate(R.layout.list_item, parent, false);
        TextView name, info;
        name = (TextView) row.findViewById(R.id.textView1);
        info = (TextView) row.findViewById(R.id.textView2);
        name.setText(pers.get(position).getName());
        info.setText(pers.get(position).getInfo());

        return (row);
    }
}

public ArrayList<Person> getPersonsForListView() {
    ArrayList<Person> personList = new ArrayList<Person>();

    for (int i = 0; i < 10; i++) {
        Person p = new Person();
        p.setName("Name" + i);
        p.setInfo("Info" + i);
        personList.add(p);
    }

    return personList;
}
}

现在下面是activity_main.xml布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>

list_item.xml 如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="22dp"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/textView1"
    android:layout_alignParentRight="true"
    android:layout_marginRight="24dp"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

请尝试与您的代码进行比较,因为它工作正常。

关于android - ListActivity 在启动时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30408878/

相关文章:

android - 从 BroadcastReceiver 接收数据后进行数据库操作

c# - 如何将 SelectedListViewItemCollection 转换为 ListViewItemCollection

android - Spinner OnItemSelectedListener 不适用于 CustomListAdapter

php - 应用程序在 JSON jparser 发出 http 请求时崩溃

javascript - 在 react-native 中自动化应用程序图标/启动画面链接(可能使用 rnpm)?

android - 协程是否在 if 条件下维持秩序?

android - 蓝牙设备扫描不完整

java - 如何获取鼠标指针下的(ListView 的)单元格?

ios - 需要修复崩溃 : thunk for @escaping @callee_guaranteed () -> () + 28 (<compiler-generated>:0) 的错误

ios - 加载大尺寸图像会使应用程序在 Swift 2.0 中崩溃