我是Android开发新手,并尝试使用Java代码创建relativeLayout.
下面是我的MainActivty.java
package com.rt.droID.rellayoutjava;import androID.app.Activity;import androID.graphics.color;import androID.os.Bundle;import androID.Widget.button;import androID.Widget.EditText;import androID.Widget.relativeLayout;import androID.Widget.TextVIEw;import androID.Widget.relativeLayout.LayoutParams;public class MainActivity extends Activity {relativeLayout r;public static LayoutParams msgDim;EditText usernameVal, passwordVal;TextVIEw message, username, password;button login;@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); init(); createMessage(); r.addVIEw(message,msgDim); setContentVIEw(r);}public voID init() { r = new relativeLayout(this); LayoutParams dimensions = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); r.setLayoutParams(dimensions); usernameVal = new EditText(this); passwordVal = new EditText(this); message=new TextVIEw(this); username = new TextVIEw(this); password = new TextVIEw(this); login = new button(this);}public voID createMessage(){ LayoutParams msgDim = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); msgDim.addRule(relativeLayout.AliGN_PARENT_left); message.setLayoutParams(msgDim); message.setText("Please login first"); message.setTextcolor(color.rgb(0,0,0));}}
但是我得到空指针异常(从Logcat下面)
Process: com.rt.droID.rellayoutjava, PID: 2471java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rt.droID.rellayoutjava/com.rt.droID.rellayoutjava.MainActivity}: java.lang.NullPointerException: Attempt to read from fIEld 'int androID.vIEw.VIEwGroup$LayoutParams.wIDth' on a null object reference at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2298) at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at androID.app.ActivityThread.access0(ActivityThread.java:144) at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at androID.os.Handler.dispatchMessage(Handler.java:102) at androID.os.Looper.loop(Looper.java:135) at androID.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:694) Caused by: java.lang.NullPointerException: Attempt to read from fIEld 'int androID.vIEw.VIEwGroup$LayoutParams.wIDth' on a null object reference at androID.vIEw.VIEwGroup$LayoutParams.<init>(VIEwGroup.java:6403) at androID.vIEw.VIEwGroup$marginLayoutParams.<init>(VIEwGroup.java:6685) at androID.Widget.relativeLayout$LayoutParams.<init>(relativeLayout.java:1345) at androID.Widget.relativeLayout.generateLayoutParams(relativeLayout.java:1104) at androID.vIEw.VIEwGroup.addVIEwInner(VIEwGroup.java:3889) at androID.vIEw.VIEwGroup.addVIEw(VIEwGroup.java:3733) at androID.vIEw.VIEwGroup.addVIEw(VIEwGroup.java:3709) at com.rt.droID.rellayoutjava.MainActivity.onCreate(MainActivity.java:24) at androID.app.Activity.performCreate(Activity.java:5933) at androID.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2251) at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) at androID.app.ActivityThread.access0(ActivityThread.java:144) at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) at androID.os.Handler.dispatchMessage(Handler.java:102) at androID.os.Looper.loop(Looper.java:135) at androID.app.ActivityThread.main(ActivityThread.java:5221) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:694)
任何指针将不胜感激.
解决方法:
首先,我建议您使用XML文件来定义布局,而不是以编程方式编写它们(您可以搜索任何类型的示例和教程).
无论如何,您的具体问题与未初始化的变量msgDim有关.将onCreate()更改为:
@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); init(); createMessage(); msgDim = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); r.addVIEw(message,msgDim); setContentVIEw(r);}
总结 以上是内存溢出为你收集整理的使用Java代码创建相对布局时出错全部内容,希望文章能够帮你解决使用Java代码创建相对布局时出错所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)