【问题标题】:Android: ListView Load gives Null Pointer ExceptionAndroid:ListView 加载给出空指针异常
【发布时间】:2013-04-09 07:55:42
【问题描述】:

我正在尝试将数据从 sqlite 获取到 listview,因此我按照此 url 中的答案中的示例进行操作 Make listview from SQLite database

CustomCursorAdapter 是这样的

    public class CustomCursorAdapter extends SimpleCursorAdapter  {
    private int layout; 
    private LayoutInflater inflater;
    private Context context;

    public CustomCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
        super(context, layout, c, from, to);
        this.layout = layout;
        this.context = context;
        inflater = LayoutInflater.from(context);

    }


    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        View v = inflater.inflate(R.layout.topics, parent, false);

        return v;
    }

    @Override
    public void bindView(View v, Context context, Cursor c) {
                //1 is the column where you're getting your data from
        String name = c.getString(1);
        /**
         * Next set the name of the entry.
         */
        TextView name_text = (TextView) v.findViewById(R.id.listLabel);
        if (name_text != null) {
            name_text.setText(name);
        }   
    }
}

但是 onCreate 我有这个代码

ListView listView = (ListView)findViewById(R.layout.topics);
    connectToDB();
    from = new String[] { "topicTitle" };
    to = new int[] { R.id.listLabel };

    CustomCursorAdapter adapter = new CustomCursorAdapter(this,
            R.layout.topics, cursor, from, to);
    listView.setAdapter(adapter);

用xml如下

    <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/listLogo"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="5dp"
         >
    </ImageView>

    <TextView
        android:id="@+id/listLabel"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="15sp" >
    </TextView>

</LinearLayout>

问题是.. listView 是空的!! ..我不知道为什么!!

【问题讨论】:

  • 发布您的 logcat 错误。
  • 在获取 listview 参考之前是否设置了 ContentView
  • 你的布局中的 ListView 在哪里?
  • 你的 ListView 在 xml 中的什么位置??
  • 您是否正在寻找自定义列表视图,其中包含每个 roe 的图像和文本视图?

标签: android sqlite nullpointerexception simplecursoradapter


【解决方案1】:

您指的是您的布局主题。您应该在 xml 中定义一个带有 id 的列表视图。 在 setCONntentView(R.layout.topics) 之后的活动 onCreate() 中,找到 listview 的 id 并将适配器设置为您的列表。

在你的xml中说topics.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" 
android:background="#0095FF">
   <ListView android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="100dp" 
    android:id="@+id/lv">
   </ListView>
    // other ui elements
</LinearLayout>

然后在你的活动中 onCreate()

   setContentview(R.layout.topics);
   ListView listView = (ListView)findViewById(R.id.lv);
   connectToDB();
   from = new String[] { "topicTitle" };
   to = new int[] { R.id.listLabel };


   CustomCursorAdapter adapter = new CustomCursorAdapter(this,
        R.layout.topics, cursor, from, to);
    listView.setAdapter(adapter);

来自聊天中的讨论

编辑:

定义 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" 
android:background="#0095FF">

<ListView android:id="@+id/list"
android:layout_width="fill_parent"  
android:layout_height="0dip"
android:focusableInTouchMode="false"
android:listSelector="@android:color/transparent"
android:layout_weight="2"
android:headerDividersEnabled="false"
android:footerDividersEnabled="false"
android:dividerHeight="8dp" 
android:divider="#000000" 
android:cacheColorHint="#000000"
android:drawSelectorOnTop="false">
</ListView>
</LinearLayout>

在你的活动中

 public class MainActivity extends Activity {

 ListView lv;
 CustomCursorAdapter cus;
 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    lv = (ListView) findViewById(R.id.list);
    cus= new CustomCursorAdapter(parameters);
    lv.setAdapter(cus);

 }
 }

定义 customCursor 适配器扩展自定义布局。为图像视图设置图像,为文本视图设置文本。

【讨论】:

  • 你的解决方案给了我错误 04-09 08:24:23.687: E/AndroidRuntime(26971): FATAL EXCEPTION: main 04-09 08:24:23.687: E/AndroidRuntime(26971): java .lang.RuntimeException:无法启动活动 ComponentInfo{com.thedarkdimension.StrangeInfo/com.thedarkdimension.StrangeInfo.Topics}:java.lang.RuntimeException:二进制 XML 文件第 9 行:您必须提供 layout_height 属性。
  • @MohamedEmadHegab 更新了我的答案。在复制粘贴 xml 代码时,我错过了列表视图的高度。添加 android:layout_height="100dp" 到您的列表视图。在 dp 或 fill_parent 中提及高度。
【解决方案2】:

尝试编辑您的 xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" 
android:id="@+id/main"
>

<ImageView
    android:id="@+id/listLogo"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="20dp"
    android:layout_marginTop="5dp"
     >
</ImageView>

<TextView
    android:id="@+id/listLabel"
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:text="@+id/label"
    android:textSize="15sp" >
</TextView>

<ListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/topics"
        ></ListView>

 </LinearLayout>

在 Activity onCreate

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ListView listView = (ListView)findViewById(R.layout.topics);
    connectToDB();
    from = new String[] { "topicTitle" };
    to = new int[] { R.id.listLabel };

    CustomCursorAdapter adapter = new CustomCursorAdapter(this,
            R.layout.topics, cursor, from, to);
    listView.setAdapter(adapter);
}

【讨论】:

  • 您的解决方案在 setContentView 04-09 08:24:23.687: E/AndroidRuntime(26971): FATAL EXCEPTION: main 04-09 08:24:23.687: E/AndroidRuntime(26971) 处给了我错误:java.lang.RuntimeException:无法启动活动 ComponentInfo{com.thedarkdimension.StrangeInfo/com.thedarkdimension.StrangeInfo.‌​Topics}:java.lang.RuntimeException:二进制 XML 文件第 9 行:您必须提供 layout_height 属性。 ——
  • @MohamedEmadHegab 我更新了我的答案。不要忘记 LinearLayout id。
  • 我认为 setContentView(R.layout.main);将是 setContentView(R.layout.topics);也
  • 现在它告诉我(您的内容必须有一个 id 属性为 'android.R.id.list 的 ListView)
  • 不! setContentView(R.layout.main),因为你的主要布局是 LinearLayout,它的 id 是 android:id="@+id/main" - 检查我的答案。
猜你喜欢
  • 1970-01-01
  • 2014-09-06
  • 2014-07-19
  • 2017-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多