片段中的对话框:

时间:2019-02-28 10:47:34

标签: java android dialog fragment

我试图在我的片段中添加一个对话框。我有一个MainActivity(“抽屉”菜单),它包含我的“片段”。在一个片段中,我想显示一个对话框,但是当我尝试更改视图中的内容或尝试实现动作侦听器时,总是得到空对象引用。

public class Fragment_Statistik extends Fragment implements View.OnClickListener {

TextView currentBerrys,currentFish,currentTrees,currentLumber,currentStones,currentStonebricks;
ProgressBar progressBarBerrys, progressBarFish, progressBarTrees,progressBarLumber,progressBarStones,progressBarStonebricks;
FloatingActionButton fab;
Button upgrade;
boolean running = true;
Dialog popupUpgradeWarehouse;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_statistik, container, false);

}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    fab = getView().findViewById(R.id.floatingActionButton);
    currentBerrys = getView().findViewById(R.id.textView_current_berrys);
    currentFish = getView().findViewById(R.id.textView_current_fish);
    currentTrees = getView().findViewById(R.id.textView_current_trees);
    currentLumber = getView().findViewById(R.id.textView_current_lumber);
    currentStones = getView().findViewById(R.id.textView_current_stone);
    currentStonebricks = getView().findViewById(R.id.textView_current_stonebricks);
    progressBarBerrys = getView().findViewById(R.id.progressBarBerrys);
    progressBarFish = getView().findViewById(R.id.progressBarFish);
    progressBarTrees = getView().findViewById(R.id.progressBarTree);
    progressBarLumber = getView().findViewById(R.id.progressBarLumber);
    progressBarStones = getView().findViewById(R.id.progressBarStone);
    progressBarStonebricks = getView().findViewById(R.id.progressBarStonebrick);
    fab.setOnClickListener(this);
    popupUpgradeWarehouse = new Dialog(getContext());}

showPopupupgradeWarehouse运行正常。删除所有其他代码后,会显示对话框。

    public void showPopupUpgradeWarehouse() {
    popupUpgradeWarehouse.setContentView(R.layout.popup_upgrade_warehouse);
    upgrade = getView().findViewById(R.id.popupButtonUpgrade);
    ImageView popupImageViewClose;
    popupImageViewClose = getView().findViewById(R.id.popupImageViewClose);

    upgrade.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        }
    });
    popupUpgradeWarehouse.show();
}

0 个答案:

没有答案