2012-04-17 16 views
0

こんにちは私は動的にビューを作成するためにnullポインタ例外(私の他の質問を参照)で私のコードに問題がありました。ここで動的レイアウトを動的に作成したときに何も表示されない

は私のコードです:私はそれを実行すると

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

作成したレイアウトはありません。

私が見逃したことはありますか?

+0

あなたのlogcat –

答えて

0

あなたは

はあなたのonCreateはこのように見えるように... RelativeLayoutオブジェクトを作成しますが、ウィンドウのコンテンツとして設定されることはありません:

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

} 
0

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

あなたの問題です。

addview(your_layout); 
関連する問題