Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 动态创建相对布局时不显示任何内容_Java_Android_Android Layout_Android Widget - Fatal编程技术网

Java 动态创建相对布局时不显示任何内容

Java 动态创建相对布局时不显示任何内容,java,android,android-layout,android-widget,Java,Android,Android Layout,Android Widget,您好,我的代码一直存在空指针异常问题(请参阅我的其他问题),因此我开始动态创建视图 这是我的密码: public class HarvestLifeActivity extends Activity implements OnTouchListener { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.

您好,我的代码一直存在空指针异常问题(请参阅我的其他问题),因此我开始动态创建视图

这是我的密码:

public class HarvestLifeActivity extends Activity implements OnTouchListener {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //add touch listeners for buttons : assign each a name and link it.
    View MoveUp = findViewById(R.id.up);
    MoveUp.setOnTouchListener(this); // use (this) to link to the current onTouchListener references in class declaration above.
    View MoveDown = findViewById(R.id.down);
    MoveDown.setOnTouchListener(this);
    View MoveLeft = findViewById(R.id.left);
    MoveLeft.setOnTouchListener(this);
    View MoveRight = findViewById(R.id.right);
    MoveRight.setOnTouchListener(this);

}

@Override
protected void onStart() {
    super.onStart();

    Drawable standing = getResources().getDrawable(R.drawable.test_circle);
    //code to convert character size from pixel to dp
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    float logicalDensity = metrics.density;

    int pxToConv = 50;
    int convToDp = (int) (pxToConv / logicalDensity + 0.5);

    RelativeLayout characterContainer = new RelativeLayout(this);
    RelativeLayout.LayoutParams characParams = new RelativeLayout.LayoutParams(convToDp, convToDp);
    characParams.addRule(RelativeLayout.CENTER_IN_PARENT);
    characterContainer.setBackgroundDrawable(standing);
    characterContainer.setId(101);




}
// Process button clicks

public boolean onTouch(View v, MotionEvent event) {
    switch(v.getId()){
        case R.id.up:
            switch (event.getAction()) {

            case MotionEvent.ACTION_DOWN:

                //as long as up button is pressed it will keep moving character up
               while (event.getAction() == MotionEvent.ACTION_DOWN)
               {    
                    RelativeLayout characterContainer = (RelativeLayout) findViewById(101);
                    Drawable walking = getResources().getDrawable(R.drawable.test_circle_red);
                    int startX = characterContainer.getLeft();
                    int startY = characterContainer.getTop();
                    int defaultWidth = characterContainer.getWidth();
                    int defaultHeight = characterContainer.getHeight();

                        //create new position for character 1 pixel closer to the top of screen
                        int newX = startX - 1;
                        int newY = startY;

                        //remove character
                        RelativeLayout view = (RelativeLayout) characterContainer;
                        ViewGroup owner = (ViewGroup) view.getParent();
                        owner.removeView(view);

                        //re make character in new position created above and assign background as walking forwards animation
                        RelativeLayout.LayoutParams characParams = new RelativeLayout.LayoutParams(defaultWidth,defaultHeight);
                        characParams.leftMargin = newY;
                        characParams.topMargin = newX;
                        characterContainer.setLayoutParams(characParams);           
                        characterContainer.setBackgroundDrawable(walking); 
                        }

                    break;

                    // when button is let go of

                case MotionEvent.ACTION_UP:

                    RelativeLayout characterContainer = (RelativeLayout) findViewById(101);
                    Drawable standing = getResources().getDrawable(R.drawable.test_circle);
                    characterContainer.setBackgroundDrawable(standing);

                    default:
                    break;
                    }
                    return true;

           case R.id.down:
               .. ..
            }
    return true;
    }
}
当我运行它时,创建的布局不在那里


我错过了什么吗

您正在创建RelativeLayout对象,但从未将其设置为窗口的内容

使您的onCreate如下所示:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Cut these lines out of onStart();
    RelativeLayout characterContainer = new RelativeLayout(this);
    RelativeLayout.LayoutParams characParams = new RelativeLayout.LayoutParams(convToDp, convToDp);
    characParams.addRule(RelativeLayout.CENTER_IN_PARENT);
    characterContainer.setBackgroundDrawable(standing);
    characterContainer.setId(101);

    setContentView(characterContainer);

    //add touch listeners for buttons : assign each a name and link it.
    /*View MoveUp = findViewById(R.id.up);
    MoveUp.setOnTouchListener(this); // use (this) to link to the current onTouchListener references in class declaration above.
    View MoveDown = findViewById(R.id.down);
    MoveDown.setOnTouchListener(this);
    View MoveLeft = findViewById(R.id.left);
    MoveLeft.setOnTouchListener(this);
    View MoveRight = findViewById(R.id.right);
    MoveRight.setOnTouchListener(this);*/

}

您没有将创建的布局添加到现有视图中

那是你的问题

addview(your_layout);