0

创建一个具有自定义列表视图的 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
4

2 回答 2

0

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

android.R.layout.simple_list_item_1

而不是您的自定义。

remindersList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values));
于 2013-10-27T07:46:05.343 回答
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

于 2013-10-27T08:33:59.830 回答