-1

I'm trying to set the size of a CardView inside of a DialogFragment, but I get this error:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.support.v7.widget.CardView.getLayoutParams()' on a null object reference

This is my code:

        CardView cardView;
        onCreate(...){
            cardView = (CardView) findViewById(R.id.cardViewInDialogFragment);
        }

        private void openDialogFragment() {

            DisplayMetrics displayMetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

            BlankFragment blankFragment = new BlankFragment();
            blankFragment.show(getSupportFragmentManager(),"blankFragment");

            CardView.LayoutParams layoutParams = (CardView.LayoutParams) cardView.getLayoutParams();
            layoutParams.height = displayMetrics.heightPixels - 20;
            layoutParams.width = displayMetrics.widthPixels - 20;
            cardView.setLayoutParams(layoutParams);
        }

Is it not possible or am I doing something wrong?

UPDATE: The cardView is inside the fragment.

UPDATE 2:

When changing the code to bellow:

BlankFragment:

public static View view;
pulic void onCreateView(...) {

    view = inflater.inflate(R.layout.fragment_blank, container, false);
    return view;
}

MainActivity:

CardView cardView;
protected void onCreate(...){
    ...

    cardView = (CardView) BlankFragment.view.findViewById(R.id.cardViewInDialogFragment);
    openDialogFragment();
}

private void openDialogFragment() {

    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

    BlankFragment blankFragment = new BlankFragment();
    blankFragment.show(getSupportFragmentManager(),"blankFragment");

    CardView.LayoutParams layoutParams = (CardView.LayoutParams) cardView.getLayoutParams();
    layoutParams.height = displayMetrics.heightPixels - 20;
    layoutParams.width = displayMetrics.widthPixels - 20;
    cardView.setLayoutParams(layoutParams);
}

, I get this error:

FATAL EXCEPTION: main Process: com.sim.buttombarnavigationandfragment, PID: 10685 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sim.buttombarnavigationandfragment/com.sim.buttombarnavigationandfragment.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2680) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2741) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6176) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference at com.sim.buttombarnavigationandfragment.MainActivity.onCreate(MainActivity.java:56) at android.app.Activity.performCreate(Activity.java:6679) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2741)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6176)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)

SIMMORSAL
  • 1,402
  • 1
  • 16
  • 32
  • 1
    cardView instance is null. Are you sure that id cardViewInDialogFragment is the right id in your inflated layout? CardView is part of the activity layout or fragment layout? – Alex Jul 03 '17 at 08:56
  • `findViewById` is outside of a method, where it will not work – OneCricketeer Jul 03 '17 at 09:09
  • @Alex it's the correct id. The CardView is inside the fragment. – SIMMORSAL Jul 03 '17 at 09:42
  • You can't make views static. You **need** to assign the CardView **within** the Fragment... `BlankFragment.view` is still null in your edit – OneCricketeer Jul 03 '17 at 13:19

1 Answers1

0
public CardView view;
public View onCreateView(...) {

View view = inflater.inflate(R.layout.fragment_blank, container, false);

cardView = (CardView) 
view.findViewById(R.id.cardViewInDialogFragment);  
openDialogFragment();
return view;

}

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
jigar savaliya
  • 474
  • 1
  • 8
  • 21
  • I created a `public static View view;` in the fragment class, and in `onCreateView` edited return to this: `return view = inflater.inflate(R.layout.fragment_blank, container, false);`, and tried to access the view using this: `cardView = (CardView) BlankFragment.view.findViewById(R.id.cardViewInDialogFragment);`, but now it throws this exception: _Unable to start activity ... java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference_ – SIMMORSAL Jul 03 '17 at 09:50
  • dont return view directly,first define view and than in the class you can put your cardview line,remove blankfragment. view = inflater.inflate(R.layout.fragment_one, container, false); return view; – jigar savaliya Jul 03 '17 at 10:09
  • just remove blankfragment in the cardview obj declaration – jigar savaliya Jul 03 '17 at 10:24
  • @jigar-savalia how do i access view if i remove the BlankFragment.? (I'm declaring and assigning view in the fragment class, and accessing it in my MainActivity class.) – SIMMORSAL Jul 03 '17 at 10:45
  • Updated the question. – SIMMORSAL Jul 03 '17 at 12:03