我在自定义视图类的构造函数中的 super(context)行上有一个NullPointerException,它扩展了LinearLayout。使用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);
}
答案 0 :(得分:1)
试试这个:
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);
}
}
答案 1 :(得分:0)
你必须改变它:
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);
}