2014-07-07 47 views
0

你好:)我有一個佈局文件的紅色背景,也是一個TextView,但是當我將這個佈局添加到我的課,我只看到一個空白頁。得到一個空白頁,而不是佈局xml文件

這裏是我的MainActivity:

@SuppressLint("NewApi") 
@SuppressWarnings("unused") 
public class MainActivity extends Activity implements SensorEventListener { 
    private SensorManager sensorManager; 
    private Sensor accelerometer; 
    private long lastUpdate; 
    AnimatedView animatedView = null; 
    ShapeDrawable mDrawable = new ShapeDrawable(); 
    public static int x; 
    public static int y; 
    public static final int width = 50; 
    public static final int height = 50; 
    public boolean firstDraw = true; 
    private int screen_width; 
    private int screen_height; 
    private int sensorX; 
    private Timer t; 
    private int TimeCounter = 0; 
    private TextView highscore_int; 
    private int sensorY; 
    private int time; 
    private SharedPreferences prefs; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     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); 
     t = new Timer(); 
     t.scheduleAtFixedRate(new TimerTask() { 
      public void run() { 
       runOnUiThread(new Runnable() { 
        public void run() { 
         TimeCounter++; 
        } 
       }); 
      } 
     }, 0, 1000); 
    } 

    @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) { 
       int oldScore = prefs.getInt("key", 0); 
       prefs = this.getSharedPreferences("myPrefsKey", 
         Context.MODE_PRIVATE); 
       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.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/actionbar_background" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/highscore_int" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:text="MyText" 
     android:textColor="@android:color/black" 
     android:textSize="40sp" /> 

</RelativeLayout> 

它並沒有真正困擾我,但我必須有一個TextView中使用的高分,我的應用程序: )

爲什麼我沒有真正的XML文件,只有一個移動球的空白頁?

+0

大衛,請開始格式化你的代碼?在Eclipse中使用ctrl + shift + f或在Android Studio中使用ctrl + shift + L ... – WarrenFaith

+0

沒有相對佈局的屬性android:orientation =「vertical」首先改變 –

+0

@WarrenFaith做了它-_- – DavidBalas

回答

1

爲什麼我沒有真正的xml文件,只有一個帶有移動 球的空白頁?

  • 因爲你已經設置的內容視圖兩次。所以後面定義的那個效果。

    @Override   
        protected void onCreate(Bundle savedInstanceState) { 
         super.onCreate(savedInstanceState); 
         setContentView(R.layout.activity_main); 
         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); 
    
         //more code 
        } 
    
+0

你是對的!但不幸的是,現在我的按鈕丟失了,所以我的應用程序是毫無價值的:( – DavidBalas

+0

@DavidBalas - 這是預期的,你應該添加視圖'animatedView'動態到父視圖 –

+0

你能幫我嗎?:) – DavidBalas

0

基於@VedPrakash答案,有評論:

這是一種簡單的。致電findViewById()以獲得您的RelativeLayout,請撥打該佈局addView(animatedView)而不是setContentView()。您將看到新的添加視圖。由於您的AnimatedView是自定義視圖,因此您可能需要檢查此資源約custom views並將該類移動到獨立文件中。

+0

沒關係..它不適合我..應用程序崩潰,因爲行your_layout。addView(animatedView); – DavidBalas

+0

碰撞意味着什麼?你有可以分享的堆棧跟蹤嗎?同時在問題中更新你的代碼以反映最新的變化 – WarrenFaith

0

在活動添加下列行下面這行

animatedView =新AnimatedView(本);

RelativeLayout layout = (RelativeLayout)findViewById(R.id.your_layout); 
layout.addView(addIcon()); 

private ImageView addIcon(){ 
    ImageView item = new ImageView(this); 
    item.setImageResource(R.drawable.tiles); 
    item.setAdjustViewBounds(true); 
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
item.setLayoutParams(params); 
    item.setId(mIconIdCounter); 
return item; 
} 

檢查該解決方案,並告知

+0

通知我一旦你達到目標 –

+0

3問題: 1)'瓷磚無法解析或不是字段' 2)哪些導入? '我需要導入LayoutParams,但有很多選項':P 3)'mIconIdCounter無法解析爲變量' – DavidBalas

相關問題