函数内部的LayoutParam问题

时间:2018-09-03 16:41:41

标签: java android android-layoutparams

我试图通过将RelativeLayout.LayoutParams放置在函数中来清理Java代码,这样就不必每次添加新东西时都创建新的layoutparams。

问题:我收到了错误代码

Unable to start activity ComponentInfo{onedevz.com.noct/onedevz.com.noct.MainActivity}: java.lang.NullPointerException: Layout parameters cannot be null

它说我没有布局参数,尽管我确实有一个,并且在将它放入Button之前我已经调用了它。这是代码

public class MainActivity extends AppCompatActivity {

    MicButton micbutton;
    Button menuBtn,onBtn;
    EditText text;
    RelativeLayout relativeLayout;
    RelativeLayout.LayoutParams lp1,lp2,lp3,lp4;
    boolean clicked;

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

// Creating a new RelativeLayout
        relativeLayout = new RelativeLayout(this);

        // Defining the RelativeLayout layout parameters.
        // In this case I want to fill its parent
        RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.FILL_PARENT);

// Defining the layout parameters of the TextView
        placement(lp1,100,100,0,200,40,0,RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.ALIGN_PARENT_LEFT);
        placement(lp2,100,100,0,350,40,0,RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.ALIGN_PARENT_LEFT);
        placement(lp3,100,100,0,500,40,0,RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.ALIGN_PARENT_LEFT);
        placement(lp4,100,ViewGroup.MarginLayoutParams.MATCH_PARENT,150,0,40,0,RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.CENTER_HORIZONTAL);

        setParameter();

        addView();
// Setting the RelativeLayout as our content view
        setContentView(relativeLayout, rlp);
}
   void placement(RelativeLayout.LayoutParams name,int height,int width,int topMargin,int bottomMargin,int leftMargin,int rightMargin,int rule1,int rule2){
        name = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        name.addRule(rule1);
        name.addRule(rule2);
        name.leftMargin = leftMargin;
        name.bottomMargin = bottomMargin;
        name.height = height;
        name.width = width;
    }

    void setParameter(){
        // Setting the parameters on the TextView
        micbutton.setLayoutParams(lp1);
        onBtn.setLayoutParams(lp2);
        menuBtn.setLayoutParams(lp3);
        text.setLayoutParams(lp4);
    }

    void addView(){
        // Adding the TextView to the RelativeLayout as a child
        relativeLayout.addView(micbutton);
        relativeLayout.addView(text);
    }
}

任何帮助将不胜感激,谢谢。

2 个答案:

答案 0 :(得分:0)

您的LayoutParameters lp1,lp2,lp3,lp4尚未初始化。初始化它们,然后将其设置为TextView

答案 1 :(得分:-1)

解决方案:

这样写:

void placement(RelativeLayout.LayoutParams name,int height,int width,int topMargin,int bottomMargin,int leftMargin,int rightMargin,int rule1,int rule2){
    name = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    name.addRule(rule1);
    name.addRule(rule2);
    name.leftMargin = leftMargin;
    name.bottomMargin = bottomMargin;
    name.height = height;
    name.width = width;

    void setParameter();
}

void setParameter(){
    // Setting the parameters on the TextView
    micbutton.setLayoutParams(lp1);
    onBtn.setLayoutParams(lp2);
    menuBtn.setLayoutParams(lp3);
    text.setLayoutParams(lp4);

    void addView();

}

void addView(){
    // Adding the TextView to the RelativeLayout as a child
    relativeLayout.addView(micbutton);
    relativeLayout.addView(text);
}

希望它能解决您的问题。