空例外 - 不知道为什么

时间:2011-10-04 20:22:24

标签: android exception null

我不明白为什么我的代码中出现了Null Exception:

 public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        if (v == null) {
            LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.lista_comunidade, null);
        }
        Message o = items.get(position);
        if (o != null) {
            TextView tt = (TextView) v.findViewById(R.id.result_name);
            TextView t = (TextView) v.findViewById(R.id.TextView01);
            TextView bt = (TextView) v.findViewById(R.id.result_second_line);
            //bt.setTextSize(TypedValue.COMPLEX_UNIT_PX, 10);
            t.setTextSize(TypedValue.COMPLEX_UNIT_PX, 10);
            bt.setTextColor(-1);
            tt.setTextColor(-1);
            tt.setTextColor(-1);

            ImageView iv = (ImageView) v.findViewById(R.id.result_icon);

            iv.setImageResource(R.id.result_icon);
            if (tt != null) {
                tt.setText(o.getTitle());
            }
            if (bt != null) {
                bt.setText("Data: " + o.getDate().toString());

                }
            if(t != null){
                t.setText(o.getLink().toString());
            }
            }


        return v;
    }
}

我的catlog说:

 E/AndroidRuntime(  757): FATAL EXCEPTION: main
 E/AndroidRuntime(  757): java.lang.NullPointerException
 E/AndroidRuntime(  757):        at com.warriorpoint.androidxmlsimple.MessageList
 $SiteAdapter.getView(MessageList.java:182)

第182行是:

 TextView t = (TextView) v.findViewById(R.id.TextView01);

我在我的xml中有这个:

  <TextView android:text="@string/Text" android:layout_width="wrap_content" android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_alignParentBottom="true"></TextView>

我缺少什么?感谢

1 个答案:

答案 0 :(得分:2)

第182行是TextView t = (TextView) v.findViewById(R.id.TextView01); 布局中的ID为@+id/textView1

您的资源中似乎有TextView01(允许代码编译),但不在当前布局中。