2011-10-20 9 views
3

CursorAdapterを継承したクラスを作成しましたが、bindViewメソッドで問題が発生しています。CursorAdapterのbindViewメソッドでNullPointerExceptionが発生しました

@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); 

} 

イム常にこのライン

のTextViewのname_text =(TextViewの)v.findViewById(R.id.qr_url)でNullPointerExceptionが取得。

私はxr_urlを自分のxmlの1つに定義しているので変です。

<?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の理由です。前もって感謝します。

+0

あなたの 'newView(View、Context、Cursor)'も投稿してください。 –

+0

これはそれに関連しています。私は私のメソッドに例外があるので、そのメソッドにnullを返しました。 NullPointerExceptionについて説明しています。ありがとう。しかし、そのメソッドのオリジナルコードは@Overrideです。 \t public View newView(Context context、Cursor cursor、ViewGroup parent){ \t \t final LayoutInflater inflater = LayoutInflater.from(context); \t \t戻り値inflater.inflate(layout、parent、false); // \t \t return null; \t} 返品のあるラインで例外があります – androidnewbie

+0

「レイアウト」はどのように定義されていますか?私の編集 – Vladimir

答えて

2

あなたはこの行のNullPointerException

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

を取得している場合は、TextViewのが発見されていない場合、原因nullが返されるとNullPointerExceptionだから

name_text.setText(colValue); 

にスローされるだろう、vがヌルであることを意味しpublic View newView(...)が正しく上書きされていることを確認してください。あなたはnewViewに返すべきか

編集 は私がbindView()はNullPointerExceptionが投げていた同様の問題を抱えていた

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

ありがとうございます。私のpublic View newView(...)メソッドに問題があると思います。私はそれが私が遭遇した他の例外に対する解決策だと思ったので、意図的にそのメソッドにnullを返しました。元の私のメソッドは:\t @Override \tパブリックビューnewView(コンテキストコンテキスト、カーソルカーソル、ViewGroup親){ \t \t final LayoutInflater inflater = LayoutInflater.from(context); \t \t戻り値inflater.inflate(layout、parent、false); // \t \t return null; \t} しかし、私はラインリターンで例外を抱えています... android.content.res.Resources $ NotFoundException:リソースID#0x0これは理由がわかりますか? – androidnewbie

+0

これは 'layout'が0であることを意味します。あなたは' layout = R.layout。<あなたの質問に投稿したXMLの名前>か、私の答えに示唆したようにR.layoutを渡すかのいずれかです。 と 'inflater.inflate()' – Vladimir

+0

が正しいです。レイアウトをR.layout.list_layout_qrurlというレイアウトで定義しました。現在、name_text.setText(colValue)にNullPointerExceptionがあります。 bindViewメソッドの – androidnewbie

0

です。私はListViewにsetEmptyView()と呼んで解決しました。

関連する問題