简体   繁体   English

Android Button setHeight()错误

[英]Android Button setHeight() error

When I am trying to do: button.setHeight(900) 当我尝试执行以下操作时: button.setHeight(900)

So the following error occurred: 因此发生以下错误:

java.lang.RuntimeException: Unable to start activity java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setHeight(int)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2423) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483) at android.app.ActivityThread.access$900(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1349) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5438) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setHeight(int)' on a null object reference at com.codeduc java.lang.RuntimeException:无法启动活动java.lang.NullPointerException:尝试在android.app.ActivityThread.performLaunchActivity(ActivityThread.com上的空对象引用上调用虚拟方法'void android.widget.Button.setHeight(int)'。 java:2423)位于android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)位于android.app.ActivityThread.access $ 900(ActivityThread.java:153)at android.app.ActivityThread $ H.handleMessage(ActivityThread.java: 1349),位于java.android.app.ActivityThread.main(ActivityThread.java:5438),位于android.os.Looper.loop(Looper.java:148),位于android.os.Handler.dispatchMessage(Handler.java:102)。 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:738)上的lang.reflect.Method.invoke(本机方法)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628) )由以下原因引起:java.lang.NullPointerException:尝试在com.codeduc上的空对象引用上调用虚拟方法'void android.widget.Button.setHeight(int)' .aayushman.apps.htmlviewerpro.MainActivity.onCreate(MainActivity.java:100) at android.app.Activity.performCreate(Activity.java:6303) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376) .aayushman.apps.htmlviewerpro.MainActivity.onCreate(MainActivity.java:100)在android.app.Activity.performCreate(Activity.java:6303)在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)在android。 app.ActivityThread.performLaunchActivity(ActivityThread.java:2376)

Please tell how to fix it. 请告诉您如何解决。

Any component available in xml must be initialized in the respective activity file. Then only it is availablein the activity scope
Button button = (Button) findViewById(R.id.assetname);  //Need to declare
then we can do as follows
button.setHeight(100)
or more logically we can have as follows
int buttonWidth = (int) (screenWidth / 3.0);
RelativeLayout.LayoutParams params = (LayoutParams) button.getLayoutParams();
params.width = buttonWidth;
params.leftMargin = buttonWidth;
button.setLayoutParams(params);

您的button变量为null,请尝试使用findviewbyId对其进行初始化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM