2

我正在尝试创建RelativeLayout 容器框以在其中包含元素并以编程方式将它们创建到RelativeLayout 中。代码在没有将元素放在 RelativeLayout 容器中的情况下运行良好,但是当我将它们放入时会使应用程序崩溃。

这是代码,有人可以帮我弄清楚我做错了什么,需要做些什么来解决这个问题:

layout = new RelativeLayout(this);

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    RelativeLayout.LayoutParams textParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams textParams2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams textParams3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams textParams4 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams progressParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams imageButtonParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams containerParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight);
    RelativeLayout.LayoutParams containerParams2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, dpContainerHeight);

    layout.setPadding(dpMargin, dpMargin, dpMargin, dpMargin);

    tv1 = new TextView(this);
    textParams1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    textParams1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    tv1.setId(1);
    tv1.setText("TextView1");

    tv2 = new TextView(this);
    textParams2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    textParams2.addRule(RelativeLayout.BELOW, tv1.getId());
    textParams2.setMargins(0, 0, 0, dpMargin);
    tv2.setId(2);
    tv2.setText("TextView2");

    containerLayout1 = new RelativeLayout(this);
    containerParams1.addRule(RelativeLayout.ALIGN_LEFT, tv2.getId());
    containerParams1.addRule(RelativeLayout.ALIGN_RIGHT, tv1.getId());
    containerParams1.addRule(RelativeLayout.BELOW, tv2.getId());
    containerParams1.setMargins(0, 0, 0, dpMargin);
    containerLayout1.setId(3);
    containerLayout1.setBackgroundResource(R.color.display_panels);

    containerLayout2 = new RelativeLayout(this);
    containerParams2.addRule(RelativeLayout.ALIGN_LEFT, containerLayout1.getId());
    containerParams2.addRule(RelativeLayout.ALIGN_RIGHT, containerLayout1.getId());
    containerParams2.addRule(RelativeLayout.BELOW, containerLayout1.getId());
    containerParams2.setMargins(0, 0, 0, dpMargin);
    containerLayout2.setId(4);
    containerLayout2.setBackgroundResource(R.color.display_panels);

    tv3 = new TextView(this);
    textParams3.addRule(RelativeLayout.ABOVE, tv4.getId());
    textParams3.addRule(RelativeLayout.ALIGN_LEFT, pb1.getId());
    tv3.setId(5);
    tv3.setText("TextView3");
    tv3.setTextAppearance(this, android.R.attr.textAppearanceSmall);

    tv4 = new TextView(this);
    textParams4.addRule(RelativeLayout.ABOVE, pb1.getId());
    textParams4.addRule(RelativeLayout.CENTER_HORIZONTAL);
    tv4.setId(6);
    tv4.setText("TextView4");
    tv4.setTextAppearance(this, android.R.attr.textAppearanceSmall);

    pb1 = new ProgressBar(this);
    progressParams1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    progressParams1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    progressParams1.addRule(RelativeLayout.LEFT_OF, ib1.getId());
    pb1.setId(7);
    pb1.setProgress(40);
    pb1.setMax(100);

    ib1 = new ImageButton(this);
    imageButtonParams1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    imageButtonParams1.addRule(RelativeLayout.ALIGN_TOP, pb1.getId());
    ib1.setId(8);
    ib1.setBackgroundResource(R.color.display_panels);
    ib1.setImageResource(R.drawable.ic_green_ok);

    layout.addView(tv1, textParams1);
    layout.addView(tv2, textParams2);
    containerLayout1.addView(tv3, textParams3);
    containerLayout1.addView(tv4, textParams4);
    containerLayout1.addView(pb1, progressParams1);
    containerLayout1.addView(ib1, imageButtonParams1);
    layout.addView(containerLayout1, containerParams1);
    layout.addView(containerLayout2, containerParams2);
    setContentView(layout, layoutParams);

这是日志猫:

07-08 11:44:31.926: E/AndroidRuntime(7533): FATAL EXCEPTION: main
07-08 11:44:31.926: E/AndroidRuntime(7533): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mobileapplicationretry/com.example.mobileapplicationretry.TestContainerActivity}: java.lang.NullPointerException
07-08 11:44:31.926: E/AndroidRuntime(7533):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-08 11:44:31.926: E/AndroidRuntime(7533):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-08 11:44:31.926: E/AndroidRuntime(7533):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-08 11:44:31.926: E/AndroidRuntime(7533):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-08 11:44:31.926: E/AndroidRuntime(7533):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 11:44:31.926: E/AndroidRuntime(7533):     at android.os.Looper.loop(Looper.java:137)
07-08 11:44:31.926: E/AndroidRuntime(7533):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-08 11:44:31.926: E/AndroidRuntime(7533):     at java.lang.reflect.Method.invokeNative(Native Method)
07-08 11:44:31.926: E/AndroidRuntime(7533):     at java.lang.reflect.Method.invoke(Method.java:511)
07-08 11:44:31.926: E/AndroidRuntime(7533):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-08 11:44:31.926: E/AndroidRuntime(7533):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-08 11:44:31.926: E/AndroidRuntime(7533):     at dalvik.system.NativeStart.main(Native Method)
07-08 11:44:31.926: E/AndroidRuntime(7533): Caused by: java.lang.NullPointerException
07-08 11:44:31.926: E/AndroidRuntime(7533):     at com.example.mobileapplicationretry.TestContainerActivity.onCreate(TestContainerActivity.java:72)
07-08 11:44:31.926: E/AndroidRuntime(7533):     at android.app.Activity.performCreate(Activity.java:5104)
07-08 11:44:31.926: E/AndroidRuntime(7533):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-08 11:44:31.926: E/AndroidRuntime(7533):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-08 11:44:31.926: E/AndroidRuntime(7533):     ... 11 more
4

2 回答 2

1

您要求从尚未创建的视图中获取 id (tv4.ge​​tId());

于 2013-07-08T10:40:42.690 回答
1

直到 tv3 的代码是完美的,现在你需要更改下面的代码。

    tv3 = new TextView(this);
    tv3.setId(5);
    tv3.setText("TextView3");
    tv3.setTextAppearance(this, android.R.attr.textAppearanceSmall);

    tv4 = new TextView(this);

    tv4.setId(6);
    tv4.setText("TextView4");
    tv4.setTextAppearance(this, android.R.attr.textAppearanceSmall);

    pb1 = new ProgressBar(this);

    pb1.setId(7);
    pb1.setProgress(40);
    pb1.setMax(100);


    ib1 = new ImageButton(this);
    imageButtonParams1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    imageButtonParams1.addRule(RelativeLayout.ALIGN_TOP, pb1.getId());
    ib1.setId(8);
    ib1.setBackgroundResource(R.color.display_panels);
    ib1.setImageResource(R.drawable.ic_green_ok);

    progressParams1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    progressParams1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    progressParams1.addRule(RelativeLayout.LEFT_OF, ib1.getId());
    textParams4.addRule(RelativeLayout.ABOVE, pb1.getId());
    textParams4.addRule(RelativeLayout.CENTER_HORIZONTAL);
    textParams3.addRule(RelativeLayout.ABOVE, tv4.getId());
    textParams3.addRule(RelativeLayout.ALIGN_LEFT, pb1.getId());


    layout.addView(tv1, textParams1);
    layout.addView(tv2, textParams2);
    containerLayout1.addView(tv3, textParams3);
    containerLayout1.addView(tv4, textParams4);
    containerLayout1.addView(pb1, progressParams1);
    containerLayout1.addView(ib1, imageButtonParams1);
    layout.addView(containerLayout1, containerParams1);
    layout.addView(containerLayout2, containerParams2);
    setContentView(layout, layoutParams);

当你得到空指针异常时,我已经改变了。我希望它对你有帮助。

于 2013-07-08T10:47:08.617 回答