使用findViewById的ListView返回null值

时间:2014-08-14 19:54:18

标签: android listview android-activity

我遇到了这个问题。实际上,我正在做的是,在SQLitedatabase中插入记录并获取这些记录以在列表视图中显示。

以下是代码:

package com.activities.sqlitedbtest;

import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.db.recordStore.Contact;
import com.db.recordStore.DBHelper;

public class ListManagerActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_manager);  **//edit and Uncommented this line**

        ListView listView = (ListView) findViewById(R.id.listView1); **//this line return null value..**

        Log.d("ListView :: ", ""+listView); **// printing null value**

        DBHelper helper = new DBHelper(this);
        List<Contact> list =  helper.getAllRecords();

        String[] records = new String[list.size()];

        Log.d("Records count ", ""+records.length);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_list_manager, records);

        listView.setAdapter(adapter);

    }
}

此外,activity_list_manager.xml为:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.activities.sqlitedbtest.ListManagerActivity"
    tools:ignore="MergeRootFrame" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>

我正在尝试在listView中显示列表。单击按钮此活动(ListManagerActivity)被调用。

这就是我所做的另一项活动:

Intent i = new Intent(this,ListManagerActivity.class);
startActivity(i);

无法理解我做错了什么。在logCat中显示以下错误:

修改

08-14 20:02:20.985: D/OpenGLRenderer(2446): TextureCache::get: create texture(0xb89b1b38): name, size, mSize = 29, 7488, 1116068
08-14 20:02:21.205: D/dalvikvm(2446): GC_CONCURRENT freed 151K, 3% free 8205K/8391K, paused 12ms+1ms, total 18ms
08-14 20:02:21.209: D/ListView ::(2446): android.widget.ListView@53517528
08-14 20:02:21.225: D/Records count(2446): 2
08-14 20:02:21.245: E/ArrayAdapter(2446): You must supply a resource ID for a TextView
08-14 20:02:21.245: D/AndroidRuntime(2446): Shutting down VM
08-14 20:02:21.249: W/dalvikvm(2446): threadid=1: thread exiting with uncaught exception (group=0xa6260288)
08-14 20:02:21.253: E/AndroidRuntime(2446): FATAL EXCEPTION: main
08-14 20:02:21.253: E/AndroidRuntime(2446): java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.widget.AbsListView.obtainView(AbsListView.java:2267)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.widget.ListView.onMeasure(ListView.java:1156)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.View.measure(View.java:15172)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:602)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:415)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.View.measure(View.java:15172)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.View.measure(View.java:15172)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.View.measure(View.java:15172)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.View.measure(View.java:15172)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.Choreographer.doCallbacks(Choreographer.java:555)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.Choreographer.doFrame(Choreographer.java:525)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.os.Handler.handleCallback(Handler.java:615)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.os.Looper.loop(Looper.java:137)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.app.ActivityThread.main(ActivityThread.java:4745)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at java.lang.reflect.Method.invokeNative(Native Method)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at java.lang.reflect.Method.invoke(Method.java:511)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-14 20:02:21.253: E/AndroidRuntime(2446):     at dalvik.system.NativeStart.main(Native Method)
08-14 20:02:21.253: E/AndroidRuntime(2446): Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
08-14 20:02:21.253: E/AndroidRuntime(2446):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
08-14 20:02:21.253: E/AndroidRuntime(2446):     ... 36 more
08-14 20:02:22.717: I/Process(2446): Sending signal. PID: 2446 SIG: 9

我用Google搜索了,但问题仍未解决。

我找到了this

4 个答案:

答案 0 :(得分:0)

您已对setContentView的电话进行了评论。 Android还没有夸大该特定活动的任何布局,因此当您尝试查找视图时,它会返回null,因为它找不到任何内容。

取消注释下面的行,应该解决。

setContentView(R.layout.activity_list_manager);

答案 1 :(得分:0)

为“分配数组”值时使用以下行

这一行是android的内置ListRow模板android.R.layout.simple_list_item_1

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, records);

答案 2 :(得分:0)

此行ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_list_manager, records);是问题所在。您不应该将布局资源ID作为第二个参数传递。你传递一个代表每一行的布局或简单地使用这个android.R.layout.simple_list_item_1

试试这个:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, records)

答案 3 :(得分:-1)

如果取消注释无效,我建议您使用ListActivity而不是Activity。

您可以在此处详细了解: http://developer.android.com/reference/android/app/ListActivity.html

XML中的ListView ID应为

  

机器人:ID =&#34; @android:ID /列表&#34;&#34;

在代码中你可以使用

  

ListView listView = getListView();