2014-07-08 3 views
0

안녕하세요 :) 내 앱에 문제가 있습니다. 두 번만 내 앱이 하나만 선택하도록하는 setContentView를 사용해야하므로 여기에 내 onCreate가 있습니다. 내가 공을 설정하고 이유를 묻는 사람을위한 contentview를 설정할 수 없습니다. (공에 하나, 레이아웃에 하나 필요합니다)

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    **setContentView(R.layout.activity_main);** (setting the layout) 
    highscore_int = (TextView) findViewById(R.id.highscore_int); 
    SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", 
      Context.MODE_PRIVATE); 
    time = prefs.getInt("key", 0); 
    highscore_int.setText("Highscore:" + time + "seconds."); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); 
    accelerometer = sensorManager 
      .getDefaultSensor(Sensor.TYPE_ACCELEROMETER); 
    lastUpdate = System.currentTimeMillis(); 
    animatedView = new AnimatedView(this); 
    **setContentView(animatedView);** (setting the ball .. 
    } 

, 내 앱이 모션 센서를 기반으로 공이다, 여기에서 onCreate 후 모든 것 :

@Override 
    protected void onResume() { 
     super.onResume(); 
     sensorManager.registerListener(this, accelerometer, 
       SensorManager.SENSOR_DELAY_GAME); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     sensorManager.unregisterListener(this); 
    } 

    @Override 
    public void onAccuracyChanged(Sensor arg0, int arg1) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void onSensorChanged(SensorEvent event) { 
     // TODO Auto-generated method stub 
     if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { 
      sensorY = (int) event.values[1]; 
      sensorX = (int) event.values[0]; 
      x -= sensorX * 3; 
      y += sensorY * 3; 
      if (x <= 0 || x >= screen_width || y <= 0 || y >= screen_height) { 
       prefs = this.getSharedPreferences("myPrefsKey", 
         Context.MODE_PRIVATE); 
       int oldScore = prefs.getInt("key", 0); 
       if (TimeCounter > oldScore) { 
        Editor edit = prefs.edit(); 
        edit.putInt("key", TimeCounter); 
        edit.commit(); 
       } 
       finish(); 
       Intent myIntent = new Intent(this, YouLost.class); 
       startActivity(myIntent); 
      } 
     } 
    } 

    public class AnimatedView extends ImageView { 

     Display display = getWindowManager().getDefaultDisplay(); 
     Point size = new Point(); 
     static final int width = 50; 
     static final int height = 50; 

     @SuppressLint("NewApi") 
     public AnimatedView(Context context) { 
      super(context); 
      // TODO Auto-generated constructor stub 
      display.getSize(size); 
      screen_width = size.x; 
      screen_height = size.y; 
      mDrawable = new ShapeDrawable(new OvalShape()); 
      mDrawable.getPaint().setColor(0xffffAC23); 
      mDrawable.setBounds(0, 0, screen_width, screen_height); 
     } 

     @Override 
     protected void onDraw(Canvas canvas) { 
      mDrawable.setBounds(x, y, x + width, y + height); 
      if (firstDraw) { 
       x = screen_width/2; 
       y = screen_height/2; 
       firstDraw = false; 
      } 
      mDrawable.draw(canvas); 
      invalidate(); 
     } 
    } 
} 

그래서 어떻게 사용할 수 있습니다 내 activity_main 레이아웃과 여전히 움직이는 공이 있습니까?

답변

0

콘텐츠보기를 설정하면 이미 존재하는 모든보기가 대체됩니다. 따라서 두 번 호출하고 두 세트를 유지할 수 없습니다. 당신은 모든 것을 가지고 1 레이아웃을 갖도록 일을 재 설계해야 할 것입니다.

+0

공 및 textView가 필요합니다. 프로그래밍 방식으로 textView를 추가하려고했지만 내 앱이 충돌합니다. 어떤 해결 방법이 있습니까? – DavidBalas

+0

? 너 나 좀 도와 줄래? – DavidBalas