2016-06-29 5 views
-1

ich auf einer benutzerdefinierte Ansicht bin arbeiten, die HorizontalScrollView und ich mache das mit folgendem Code erweitert:Kontext ist null in benutzerdefinierter Ansicht

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); 
    } 
} 

aber ich diesen Fehler, wenn die Anwendung ausgeführt wird:

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) 

Nach dem Debuggen bemerkte ich, dass der Kontext null ist, aber warum? Was ist das Problem?

+0

'this.context = context;' Setzen Sie diese Zeile in jeden Konstruktor und führen Sie sie erneut aus. –

+2

Warum verwenden Sie 'this.context = context;'? Sie können immer einen Kontext mit 'View.getContext()' erhalten – Selvin

Antwort

2

Sie initialisieren nur Ihre context Mitgliedsvariable in einem Ihrer drei Konstruktoren und anscheinend wurde einer der beiden anderen aufgerufen. In einer benutzerdefinierten Ansicht müssen Sie keine Context Referenz selbst speichern. Rufen Sie einfach getContext() an, wenn Sie eine Context benötigen.

+0

omg danke, ich sollte dies (context, null, 0) in anderen Konstruktoren nicht super verwenden –

Verwandte Themen