2015-05-23 76 views
0

我有一个基本的ListActivity与一些假的测试数据。当我运行应用程序时,它立即崩溃。我尝试过调试,但没有得到任何反馈。它在开始说明之后就停止了:意图。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 - 我清理该项目,现在它工作

+0

发布您的logcat异常消息。 – dora

+0

logcat在哪里? – Vijay

+0

@为你的构造函数赋予一些不可识别的第二个参数。发布Person类,让知道第二个参数是什么类型。 – dora

回答

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> 

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

+0

@rand它对你有帮助吗?请回答是或否。 –

+0

这几乎就是我发布的,它不工作。 – Rand

0

我清理了这个项目,现在所有的东西都可以使用。

+0

好的,很高兴知道。 –