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)
No comments:
Post a Comment