【问题标题】:NullPointerException on CursorAdapter bindView methodCursorAdapter bindView 方法上的 NullPointerException
【发布时间】:2023-03-21 23:40:03
【问题描述】:

我创建了一个扩展 CursorAdapter 的类,我在 bindView 方法上有问题。

@Override
public void bindView(View v, Context context, Cursor c) {


    int colNum = c.getColumnIndex(VidCallDbAdapter.QRLINK);
    String colValue = c.getString(colNum);

    System.out.println("value>> " + colValue);

    TextView name_text = (TextView) v.findViewById(R.id.qr_url);

    name_text.setText(colValue);

}

我总是在这一行得到一个 NullPointerException

TextView name_text = (TextView) v.findViewById(R.id.qr_url);

这很奇怪,因为我在我的一个 xml 中定义了 qr_url。

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

<TextView android:layout_height="wrap_content" 
          android:text=""   
          android:id="@+id/qr_url" 
          android:gravity="center_vertical" 
          android:layout_width="fill_parent" 
          android:textColor="#000000" 
          android:textSize="12sp">
</TextView>

</LinearLayout>  

我是否错过了代码中的某些内容,这就是为什么它是 NullPointerException?提前致谢。

【问题讨论】:

  • 也发布您的newView(View,Context,Cursor)
  • 哇,它与它有关。我将 null 返回给该方法,因为我的方法有异常。这解释了 NullPointerException。谢谢。但我对该方法的原始代码是 @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { final LayoutInflater inflater = LayoutInflater.from(context);返回inflater.inflate(布局,父级,假); // 返回空值; } 我在返回时遇到异常
  • layout 是如何定义的?检查我的编辑
  • 哪个编辑?布局定义为 layout = R.layout.list_layout_qrurl;在构造函数上。

标签: android android-cursoradapter


【解决方案1】:

如果你在这行得到NullPointerException

TextView name_text = (TextView) v.findViewById(R.id.qr_url);

这意味着v 为空,因为如果没有找到TextView,将返回null 并抛出NullPointerException

name_text.setText(colValue);

所以请确保您的public View newView(...) 被正确覆盖。

编辑 你应该在 newView 中返回的是

return inflater.inflate(R.layout.<name of your xml>, parent, false);

【讨论】:

  • 谢谢。我认为我的 public View newView(...) 方法有问题。我故意将 null 返回到该方法,因为我认为这是我遇到的另一个异常的解决方案。原来我的方法是:@Override public View newView(Context context, Cursor cursor, ViewGroup parent) { final LayoutInflater inflater = LayoutInflater.from(context);返回inflater.inflate(布局,父级,假); // 返回空值; } 但我在行返回时遇到异常... android.content.res.Resources$NotFoundException: Resource ID #0x0 你知道这是什么原因吗?
  • 这意味着 layout 为 0。您可以将其设置为 layout = R.layout.&lt;name of your xml, the one you posted in your question&gt; 或按照我在回答中的建议将 R.layout. 传递给 inflater.inflate()
  • 你是对的。我将布局定义为 layout = R.layout.list_layout_qrurl。现在在 name_text.setText(colValue); 中有一个 NullPointerException bindView 方法。
  • 那么colValuename_text 为空。如果 ID 为 qr_url 的 TextView 确实在 list_layout_qrurl.xml 中,请尝试 name_text.setText("test") 看看会发生什么。如果它有效,那么colValue 就有问题,这真的很奇怪
  • 我看到了问题。这是我对布局的定义(愚蠢的错误:))由于您的回复,我能够弄清楚一切。非常感谢。
【解决方案2】:

我遇到了类似的问题,bindView() 抛出 NullPointerException。我通过在我的 ListView 上调用 setEmptyView() 解决了这个问题。

【讨论】:

    猜你喜欢
    • 2012-06-05
    • 2012-08-26
    • 2013-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-01
    相关资源
    最近更新 更多