android - 由于listview,应用程序崩溃

时间:2013-10-27 05:08:11

标签: java android android-listview android-logcat

创建一个具有自定义列表视图的Android应用程序。由于活动具有其他UI元素,因此无法通过扩展ListViewActivity来实现ListView。当我的活动开始时,我在LogCat中得到以下错误。

MainActivity.java

public class MainActivity extends Activity implements OnClickListener
{
private ListView remindersList;
private String[] values = {"Item1", "Item2", "Item3"}; 
    List<Integer> allIDsFromDatabase = new ArrayList<Integer>();

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    remindersList = (ListView) findViewById(R.id.listView1);
            remindersList.setAdapter(new ArrayAdapter<String>(this, R.layout.list_item, values));
            remindersList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {

            Log.d("click arg3", "item clicked : " + arg3);
        }
    });
            remindersList.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {

            Log.d("long click", "long pressed : " + arg3);
            return false;
        }
    });
}

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"
    tools:context=".MainActivity" >
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:src="@drawable/minussmall" android:contentDescription="@string/logo"/>
<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/imageView1" >
</ListView>
</RelativeLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/textview"/>

LogCat详细信息

10-27 10:11:14.887: E/AndroidRuntime(630): FATAL EXCEPTION: main
10-27 10:11:14.887: E/AndroidRuntime(630): java.lang.NullPointerException
10-27 10:11:14.887: E/AndroidRuntime(630):  at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:355)
10-27 10:11:14.887: E/AndroidRuntime(630):  at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
10-27 10:11:14.887: E/AndroidRuntime(630):  at android.widget.AbsListView.obtainView(AbsListView.java:1409)
10-27 10:11:14.887: E/AndroidRuntime(630):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
10-27 10:11:14.887: E/AndroidRuntime(630):  at android.widget.ListView.onMeasure(ListView.java:1127)
10-27 10:11:14.887: E/AndroidRuntime(630):  at android.view.View.measure(View.java:8313)
10-27 10:11:14.887: E/AndroidRuntime(630):  at android.widget.RelativeLayout.measureChild(RelativeLayout.java:566)
10-27 10:11:14.887: E/AndroidRuntime(630):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)
10-27 10:11:14.887: E/AndroidRuntime(630):  at android.view.View.measure(View.java:8313)
10-27 10:11:14.887: E/AndroidRuntime(630):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-27 10:11:14.887: E/AndroidRuntime(630):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
10-27 10:11:14.887: E/AndroidRuntime(630):  at android.view.View.measure(View.java:8313)
10-27 10:11:14.887: E/AndroidRuntime(630):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
10-27 10:11:14.887: E/AndroidRuntime(630):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
10-27 10:11:14.887: E/AndroidRuntime(630):  at android.view.View.measure(View.java:8313)
10-27 10:11:14.887: E/AndroidRuntime(630):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
10-27 10:11:14.887: E/AndroidRuntime(630):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
10-27 10:11:14.887: E/AndroidRuntime(630):  at android.view.View.measure(View.java:8313)
10-27 10:11:14.887: E/AndroidRuntime(630):  at android.view.ViewRoot.performTraversals(ViewRoot.java:839)
10-27 10:11:14.887: E/AndroidRuntime(630):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
10-27 10:11:14.887: E/AndroidRuntime(630):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-27 10:11:14.887: E/AndroidRuntime(630):  at android.os.Looper.loop(Looper.java:123)
10-27 10:11:14.887: E/AndroidRuntime(630):  at android.app.ActivityThread.main(ActivityThread.java:3683)
10-27 10:11:14.887: E/AndroidRuntime(630):  at java.lang.reflect.Method.invokeNative(Native Method)
10-27 10:11:14.887: E/AndroidRuntime(630):  at java.lang.reflect.Method.invoke(Method.java:507)
10-27 10:11:14.887: E/AndroidRuntime(630):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-27 10:11:14.887: E/AndroidRuntime(630):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-27 10:11:14.887: E/AndroidRuntime(630):  at dalvik.system.NativeStart.main(Native Method)
10-27 10:11:30.425: I/Process(630): Sending signal. PID: 630 SIG: 9

2 个答案:

答案 0 :(得分:0)

由于您没有实现自定义ArrayAdapter,请尝试使用:

android.R.layout.simple_list_item_1

而不是自定义的。

remindersList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values));

答案 1 :(得分:0)

  

由于活动有其他UI元素,我无法通过扩展ListViewActivity来实现ListView。

其实你可以。但不是ListViewActivity,它的ListActivity。
你可以实现

public class MainActivity extends ListActivity

如果您将ListView的id设为

<ListView
    android:id="@android:id/list"

您现在关注的方式也是正确的。正如Rick Falck所说,将ArrayAdaper视图更改为android.R.layout.simple_list_item_1