自定义视图中的上下文为null

时间:2016-06-29 11:27:10

标签: java android layout

我正在处理扩展HorizontalScrollView的自定义视图,并且我正在使用以下代码执行此操作:

public BottomNavigation(Context context) {
    super(context, null, 0);
}

public BottomNavigation(Context context, AttributeSet attrs) {
    super(context, attrs, 0);
}

public BottomNavigation(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.context = context;
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    createItems();
}

public void createItems() {
    if (items < 4) {
        return;
    }
    height = getHeight();
    width = getWidth();
    paddingTop = height / 7;
    itemsHeight = height - paddingTop;
    itemsWidth = width / 4;
    requestLayout();
    views.clear();
    removeAllViews();
    LinearLayout linearLayout = new LinearLayout(context);
    linearLayout.setOrientation(LinearLayout.HORIZONTAL);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(height, ViewGroup.LayoutParams.WRAP_CONTENT);
    linearLayout.setLayoutParams(layoutParams);
    addView(linearLayout);
    for(int i=0;i<items;i++)
    {
        LinearLayout linearLayout1 = new LinearLayout(context);
        LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(height, itemsWidth);
        linearLayout1.setLayoutParams(layoutParams1);
        linearLayout1.setOrientation(LinearLayout.VERTICAL);
    }
}

但我在运行应用程序时遇到此错误:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.amir_p.bottomnavigation3/com.example.amir_p.bottomnavigation3.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2510)
      at android.app.ActivityThread.-wrap11(ActivityThread.java)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:148)
      at android.app.ActivityThread.main(ActivityThread.java:5459)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
      at android.view.ViewConfiguration.get(ViewConfiguration.java:366)
      at android.view.View.<init>(View.java:3794)
      at android.view.View.<init>(View.java:3898)
      at android.view.ViewGroup.<init>(ViewGroup.java:573)
      at android.widget.LinearLayout.<init>(LinearLayout.java:203)
      at android.widget.LinearLayout.<init>(LinearLayout.java:199)
      at android.widget.LinearLayout.<init>(LinearLayout.java:195)
      at android.widget.LinearLayout.<init>(LinearLayout.java:191)
      at com.example.amir_p.bottomnavigation3.BottomNavigation.createItems(BottomNavigation.java:68)
      at com.example.amir_p.bottomnavigation3.BottomNavigation.setItems(BottomNavigation.java:108)

调试后我发现上下文为空但为什么?有什么问题?

1 个答案:

答案 0 :(得分:2)

您只在三个构造函数中的一个中初始化context成员变量,并且显然调用了其中两个中的一个。

在自定义视图中,您无需自行存储Context引用。只要您需要getContext(),就可以致电Context