用垂直和水平线/视图划分屏幕

时间:2013-11-26 19:34:50

标签: java android view nullpointerexception

以下是我想要完成的事情。

enter image description here

绿线是一条垂直线,它将是一个简单的视图。红线将是一条水平线,也是一个简单的视图。我想要从RelativeLayout顶部的红线X量dps /像素。然后我希望绿线水平居中,然后从RelativeLayout的顶部伸展到“红线上方”。

下面是我的代码,它编译但产生运行时错误。

public class Puzzle extends Activity {

    RelativeLayout relativelayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {        
        super.onCreate(savedInstanceState);

        relativelayout = new RelativeLayout(getApplicationContext());

        dividers();

        setContentView(relativelayout);
    }

    public void dividers() {

        // horizontal line
        View view0 = new View(this);
        view0.setBackgroundColor(0xFFC2BEBF);
        relativelayout.addView(view0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 2));
        view0.setId(0);

        RelativeLayout.LayoutParams params0 = null;
        params0.addRule(RelativeLayout.CENTER_VERTICAL);    // line 39
        view0.setLayoutParams(params0);

        // vertical line
        View view1 = new View(this);
        view1.setBackgroundColor(0xFFC2BEBF);
        relativelayout.addView(view1, new ViewGroup.LayoutParams(2, ViewGroup.LayoutParams.MATCH_PARENT));

        RelativeLayout.LayoutParams params1 = null;
        params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
        params1.addRule(RelativeLayout.ABOVE, view0.getId());
        view1.setLayoutParams(params1);
    }
}

以下是LogCat输出。

11-22 00:25:02.500: E/AndroidRuntime(4140): FATAL EXCEPTION: main
11-22 00:25:02.500: E/AndroidRuntime(4140): Process: matt.lyons.tileslider, PID: 4140
11-22 00:25:02.500: E/AndroidRuntime(4140): java.lang.RuntimeException: Unable to start activity ComponentInfo{matt.lyons.tileslider/matt.lyons.tileslider.Puzzle}: java.lang.NullPointerException
11-22 00:25:02.500: E/AndroidRuntime(4140):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
11-22 00:25:02.500: E/AndroidRuntime(4140):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
11-22 00:25:02.500: E/AndroidRuntime(4140):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
11-22 00:25:02.500: E/AndroidRuntime(4140):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
11-22 00:25:02.500: E/AndroidRuntime(4140):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-22 00:25:02.500: E/AndroidRuntime(4140):     at android.os.Looper.loop(Looper.java:137)
11-22 00:25:02.500: E/AndroidRuntime(4140):     at android.app.ActivityThread.main(ActivityThread.java:4998)
11-22 00:25:02.500: E/AndroidRuntime(4140):     at java.lang.reflect.Method.invokeNative(Native Method)
11-22 00:25:02.500: E/AndroidRuntime(4140):     at java.lang.reflect.Method.invoke(Method.java:515)
11-22 00:25:02.500: E/AndroidRuntime(4140):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-22 00:25:02.500: E/AndroidRuntime(4140):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-22 00:25:02.500: E/AndroidRuntime(4140):     at dalvik.system.NativeStart.main(Native Method)
11-22 00:25:02.500: E/AndroidRuntime(4140): Caused by: java.lang.NullPointerException
11-22 00:25:02.500: E/AndroidRuntime(4140):     at matt.lyons.tileslider.Puzzle.dividers(Puzzle.java:39)
11-22 00:25:02.500: E/AndroidRuntime(4140):     at matt.lyons.tileslider.Puzzle.onCreate(Puzzle.java:24)
11-22 00:25:02.500: E/AndroidRuntime(4140):     at android.app.Activity.performCreate(Activity.java:5243)
11-22 00:25:02.500: E/AndroidRuntime(4140):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-22 00:25:02.500: E/AndroidRuntime(4140):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
11-22 00:25:02.500: E/AndroidRuntime(4140):     ... 11 more

我已经在我的代码中评论了第39行。我是新手在没有XML的情况下在Java中进行整个布局所以我对如何做到这一点感到有点困惑所以任何帮助都是值得赞赏的。为什么我得到这个NPE?

3 个答案:

答案 0 :(得分:1)

你可以Initialize params0如下所示你可以使用addRule

params0 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 2);

答案 1 :(得分:1)

尝试这个你可以根据你改变宽度,高度和颜色。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >

<View
    android:layout_width="0.5dp"
    android:layout_height="100dp"
    android:layout_gravity="center_horizontal"
    android:background="#DF5555" />

<View
    android:layout_width="match_parent"
    android:layout_height="0.5dp"
    android:layout_gravity="center_horizontal"
    android:background="#E66666" />
</LinearLayout>

答案 2 :(得分:0)

RelativeLayout.LayoutParams params0 = **null**;
   // param0 variable is not initialise 
    params0.addRule(RelativeLayout.CENTER_VERTICAL);    // line 39
    view0.setLayoutParams(params0);

也是这部分

RelativeLayout.LayoutParams params1 = **null**;
    params1.addRule(RelativeLayout.CENTER_HORIZONTAL);

初始化部分

 RelativeLayout.LayoutParams params0 = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);;