gpt4 book ai didi

java - 当 onCreate 中完成某些操作(不是声明或初始化)但 onClick 中未完成某些操作时,应用程序崩溃

转载 作者:行者123 更新时间:2023-12-02 04:58:22 31 4
gpt4 key购买 nike

我正在创建一个计算器。如你所知,它有很多按钮,我想根据屏幕尺寸调整大小。我将代码放在 onCreate 中,但它崩溃了。我将它放在 OnClickListner 中,它起作用了。我希望它能够当我们开始 Activity 时发生。我该如何修复它?

我首先在一个单独的类中执行此操作,然后在 MainActivity.java 中执行一个单独的函数。然后在 onCreate 中执行此操作。然后在 OnClickListner 中执行。

我在前 3 次尝试中访问了 onCreate 中的所有内容。

这是我在 onClickListner 中调整其大小的代码--

findViewById(R.id.btnfix).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = getApplicationContext();
Button btn1 = (Button)findViewById(R.id.b1);Button btn2 = (Button) findViewById(R.id.b2);Button btn3 = (Button) findViewById(R.id.b3);Button btn4 = (Button) findViewById(R.id.b4);Button btn5 = (Button) findViewById(R.id.b5);Button btn6 = (Button) findViewById(R.id.b6);Button btn7 = (Button) findViewById(R.id.b7);Button btn8 = (Button) findViewById(R.id.b8);Button btn9 = (Button) findViewById(R.id.b9);Button btn0 = (Button) findViewById(R.id.b0);Button btnsqrt = (Button) findViewById(R.id.sqrt);Button btnexpo = (Button) findViewById(R.id.expo);Button btncb = (Button) findViewById(R.id.cb);Button btnob = (Button) findViewById(R.id.ob);Button btnadd = (Button) findViewById(R.id.add);Button btnsub = (Button) findViewById(R.id.sub);Button btnmul = (Button) findViewById(R.id.mul);Button btndiv = (Button) findViewById(R.id.div);Button btnclr = (Button) findViewById(R.id.clr);Button btnlog = (Button) findViewById(R.id.log);


DisplayMetrics dm = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int btnw = width/4;

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) btn1.getLayoutParams();
params.width = btnw;
btn1.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btn2.getLayoutParams();
params.width = btnw;
btn2.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btn3.getLayoutParams();
params.width = btnw;
btn3.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btn4.getLayoutParams();
params.width = btnw;
btn4.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btn5.getLayoutParams();
params.width = btnw;
btn5.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btn6.getLayoutParams();
params.width = btnw;
btn6.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btn7.getLayoutParams();
params.width = btnw;
btn7.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btn8.getLayoutParams();
params.width = btnw;
btn8.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btn9.getLayoutParams();
params.width = btnw;
btn9.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btn0.getLayoutParams();
params.width = btnw;
btn0.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btnadd.getLayoutParams();
params.width = btnw;
btnadd.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btnsub.getLayoutParams();
params.width = btnw;
btnsub.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btnmul.getLayoutParams();
params.width = btnw;
btnmul.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btndiv.getLayoutParams();
params.width = btnw;
btndiv.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btnsqrt.getLayoutParams();
params.width = btnw;
btnsqrt.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btnexpo.getLayoutParams();
params.width = btnw;
btnexpo.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btnob.getLayoutParams();
params.width = btnw;
btnob.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btncb.getLayoutParams();
params.width = btnw;
btncb.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btnlog.getLayoutParams();
params.width = btnw;
btnlog.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btnclr.getLayoutParams();
params.width = btnw;
btnclr.setLayoutParams(params);
}
});

这仅在 onCreate 中--

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

textArea = (TextView) findViewById(R.id.edit_text);
errorMessageText = (TextView) findViewById(R.id.error_message);
errorMessageText.setVisibility(View.GONE);
Context context = getApplicationContext();
Button btn1 = (Button)findViewById(R.id.b1);Button btn2 = (Button) findViewById(R.id.b2);Button btn3 = (Button) findViewById(R.id.b3);Button btn4 = (Button) findViewById(R.id.b4);Button btn5 = (Button) findViewById(R.id.b5);Button btn6 = (Button) findViewById(R.id.b6);Button btn7 = (Button) findViewById(R.id.b7);Button btn8 = (Button) findViewById(R.id.b8);Button btn9 = (Button) findViewById(R.id.b9);Button btn0 = (Button) findViewById(R.id.b0);Button btnsqrt = (Button) findViewById(R.id.sqrt);Button btnexpo = (Button) findViewById(R.id.expo);Button btncb = (Button) findViewById(R.id.cb);Button btnob = (Button) findViewById(R.id.ob);Button btnadd = (Button) findViewById(R.id.add);Button btnsub = (Button) findViewById(R.id.sub);Button btnmul = (Button) findViewById(R.id.mul);Button btndiv = (Button) findViewById(R.id.div);Button btnclr = (Button) findViewById(R.id.clr);Button btnlog = (Button) findViewById(R.id.log);


DisplayMetrics dm = new DisplayMetrics();
WindowManager windowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);
windowManager.getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int btnw = width/4;

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) btn1.getLayoutParams();
params.width = btnw;
btn1.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btn2.getLayoutParams();
params.width = btnw;
btn2.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btn3.getLayoutParams();
params.width = btnw;
btn3.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btn4.getLayoutParams();
params.width = btnw;
btn4.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btn5.getLayoutParams();
params.width = btnw;
btn5.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btn6.getLayoutParams();
params.width = btnw;
btn6.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btn7.getLayoutParams();
params.width = btnw;
btn7.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btn8.getLayoutParams();
params.width = btnw;
btn8.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btn9.getLayoutParams();
params.width = btnw;
btn9.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btn0.getLayoutParams();
params.width = btnw;
btn0.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btnadd.getLayoutParams();
params.width = btnw;
btnadd.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btnsub.getLayoutParams();
params.width = btnw;
btnsub.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btnmul.getLayoutParams();
params.width = btnw;
btnmul.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btndiv.getLayoutParams();
params.width = btnw;
btndiv.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btnsqrt.getLayoutParams();
params.width = btnw;
btnsqrt.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btnexpo.getLayoutParams();
params.width = btnw;
btnexpo.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btnob.getLayoutParams();
params.width = btnw;
btnob.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btncb.getLayoutParams();
params.width = btnw;
btncb.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btnlog.getLayoutParams();
params.width = btnw;
btnlog.setLayoutParams(params);

params = (LinearLayout.LayoutParams) btnclr.getLayoutParams();
params.width = btnw;
btnclr.setLayoutParams(params);

我尝试了n次,但只在onClickListner中工作。在其他时间它崩溃了

最佳答案

它崩溃是因为您在布局尚未创建时尝试获取 LayoutParams。尝试将 Post 添加到您的父布局并在其中执行逻辑。

例如

parentLayout.post(new Runnable() {
@Override
public void run() {
//your code here
}
});

关于java - 当 onCreate 中完成某些操作(不是声明或初始化)但 onClick 中未完成某些操作时,应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56400562/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com