有一个空白页而不是布局xml文件

时间:2014-07-07 14:37:02

标签: android xml

你好:)我有一个带红色背景的布局文件,还有一个TextView但是当我把这个布局添加到我的班级时,我只看到一个空白页面。

这是我的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文件,只有一个带有移动球的空白页?

3 个答案:

答案 0 :(得分: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
         }
    

答案 1 :(得分:0)

根据@VedPrakash回答及其中的评论:

有点简单。致电findViewById()以获取RelativeLayout,在该布局上调用addView(animatedView)而不是setContentView()。然后,您将看到新添加的视图。由于您的AnimatedView是自定义视图,因此您可能需要检查此资源是否为custom views,并将该类移至独立文件中。

答案 2 :(得分:0)

在此行下方的活动中添加以下行

animatedView = new AnimatedView(this);

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

检查此解决方案并通知