0

こんにちは、ヌル ポインターの例外でコードに問題がありました (他の質問を参照してください)。そのため、ビューを動的に作成することにしました。

これが私のコードです:

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;
    }
}

実行すると、作成されたレイアウトがありません。

見逃したものはありますか?

4

2 に答える 2

0

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);*/

}
于 2012-04-17T01:45:03.293 に答える
0

作成したレイアウトを既存のビューに追加していません。

それはあなたの問題だ。

addview(your_layout);
于 2012-04-17T04:30:56.420 に答える