ListActivity在开始

时间:2015-05-23 04:26:55

标签: android listview crash

我有一个带有一些假测试数据的基本ListActivity。当我运行应用程序时,它立即崩溃。我试过调试,但我没有得到任何反馈。它只是在说开始后停止:意图。

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 - 我清理了项目,现在可以正常使用

2 个答案:

答案 0 :(得分:0)

当我按照以下方式创建项目演示时:

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>

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

答案 1 :(得分:0)

我清理了项目,现在一切正常。