-1

我正在尝试设置 aCardView内部的大小 a DialogFragment,但出现此错误:

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'android.view.ViewGroup$LayoutParams android.support.v7.widget.CardView.getLayoutParams()'

这是我的代码:

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

这是不可能的还是我做错了什么?

更新: cardView 在片段内。

更新 2:

将代码更改为波纹管时:

空白片段:

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

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

主要活动:

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

,我得到这个错误:

致命异常:主进程:com.sim.buttombarnavigationandfragment,PID:10685 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.sim.buttombarnavigationandfragment/com.sim.buttombarnavigationandfragment.MainActivity}:java.lang.NullPointerException:尝试在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2680) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2741) 在 android.app.ActivityThread.-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488) 在 android .os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread.java:6176) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 888)在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778) 引起:java.lang.NullPointerException:尝试调用虚拟方法 'android.view.View android.view.View.findViewById(int)' 在 com.sim.buttombarnavigationandfragment.MainActivity.onCreate(MainActivity.java:56) 在 android.app.Activity.performCreate(Activity.java:6679) 在 android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1119) 在 android.app 的 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2633)。ActivityThread.handleLaunchActivity(ActivityThread.java:2741) 在 android.app.ActivityThread.-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1488) 在 android.os.Handler.dispatchMessage( Handler.java:102) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread.java:6176) 在 java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) 在 com.android.internal.os.ZygoteInit.main( ZygoteInit.java:778)第888章)第888章)

4

1 回答 1

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;

}

于 2017-07-03T09:09:14.543 回答