0

我一直在无缘无故地挣扎,我不明白为什么。这是我的java代码:

public class SousRubrique extends RelativeLayout {
private View sousRubriqueView;
private TextView titleTextView;
public SousRubrique(Context context, AttributeSet attrs, int defStyle) {
    super(context);
    init(context);
}
public SousRubrique(Context context, AttributeSet attrs) {
    super(context);
    init(context);
}
public SousRubrique(Context context) {
    super(context);
    init(context);
}

// Initialization methods

    public void init(Context context){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        setSousRubriqueView(inflater.inflate(R.layout.sousrubrique, this));

        bind();
    }
    public void bind(){
        setTitleTextView((TextView) sousRubriqueView.findViewById(R.id.sousRubriqueTitle));
    }
    // end

    public void setTitle(String title){
        titleTextView.setText(title);
    }
    public TextView getTitleTextView() {
        return titleTextView;
    }
    public void setTitleTextView(TextView titleTextView) {
        this.titleTextView = titleTextView;
    }
    public View getSousRubriqueView() {
        return sousRubriqueView;
    }
    public void setSousRubriqueView(View sousRubriqueView) {
        this.sousRubriqueView = sousRubriqueView;
    }
}

这是我的 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/red"
android:orientation="vertical" >

<TextView
    android:id="@+id/sousRubriqueTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Sous-Rubrique"
    android:layout_centerInParent="true"
    android:textColor="@android:color/white" />

为什么 Eclipse 告诉我 bind 方法存在 java 空指针问题?

4

1 回答 1

1

sousRubriqueView对象永远不会被初始化。这就是为什么你得到NPE

于 2013-01-03T16:14:26.777 回答