android 2.3.3上的LinearLayout super(上下文)异常

android 2.3.3上的LinearLayout super(上下文)异常,android,android-linearlayout,superclass,android-2.3-gingerbread,Android,Android Linearlayout,Superclass,Android 2.3 Gingerbread,在扩展LinearLayout的自定义视图类的构造函数中,super(context)行上有一个NullPointerException。通过使用android 4.1.2虚拟设备进行测试,问题不会出现 public class customView extends LinearLayout { private NavigationBar navigationBar; private Activity activity; public customView(Context conte

在扩展LinearLayout的自定义视图类的构造函数中,super(context)行上有一个NullPointerException。通过使用android 4.1.2虚拟设备进行测试,问题不会出现

public class customView extends LinearLayout {
  private NavigationBar navigationBar;
  private Activity activity;
  public customView(Context context) {
    super(context);
    activity = (Activity) context;
    navigationBar = new NavigationBar(context);
    LayoutParams Params = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    setLayoutParams(Params);
    setOrientation(LinearLayout.VERTICAL);
    this.addView(navigationBar);
}

您必须使用以下选项进行更改:

public class customView extends LinearLayout {
      private NavigationBar navigationBar;

      public customView(Context context)
   {
         super(context);


     navigationBar = new NavigationBar(context);
     LayoutParams Params = new LayoutParams(LayoutParams.FILL_PARENT,   LayoutParams.FILL_PARENT);
     setLayoutParams(Params);
     setOrientation(LinearLayout.VERTICAL);

     this.addView(navigationBar);
}
试试这个:

public class MyLinearLayout extends LinearLayout
{

    public MyLinearLayout(Context context)
    {
        super(context);
        Init(context);
    }

    public MyLinearLayout(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        Init(context);
    }

    private void Init(Context context)
    {
        activity = (Activity) context;

        navigationBar = new NavigationBar(context);
        LayoutParams Params = new LayoutParams(LayoutParams.FILL_PARENT,         LayoutParams.FILL_PARENT);
        setLayoutParams(Params);
        setOrientation(LinearLayout.VERTICAL);

        this.addView(navigationBar);
    }
}

否必须定义隐式超级构造函数LinearLayout()。我发现了错误。我在自定义LinearLayout类中错误地重写了invalidate()方法。