-1

Hey :) I have a code of a ball that using the motion sensor and I want to make my app count time and when you lose, save this time for a record, so I did it, but my problem is, I can't extend my fragment layout, here is my full code:

    package com.example.theball;
import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ActivityInfo;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
@SuppressLint("NewApi") public class MainActivity extends Activity implements SensorEventListener {
        private SensorManager sensorManager;
        private Sensor accelerometer;
        @SuppressWarnings("unused")
        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 int sensorY;
        private static int score = 0;
        private static SharedPreferences prefs;
        @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);
            t=new Timer();
            t.scheduleAtFixedRate(new TimerTask() {
                public void run() {
                    runOnUiThread(new Runnable() {
                        public void run() {                 
                            TimeCounter++;      
                        }
                       });
                    }
            }, 0, 1000); 
        }
        public static class MainFragment extends Fragment {
            @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
              View v = inflater.inflate(R.layout.activity_main, container, false);
              TextView highscore_int = (TextView)v.findViewById(R.id.highscore_int );
              score = prefs.getInt("key", 0);
              highscore_int.setText("Highscore:" + score +" seconds."); 
              return v;
        }
        }
        @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) { 
                    SharedPreferences 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(); }
                    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(); 
            } 
            }
}

XML FILE:

    <LinearLayout 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:orientation="vertical" >

    <TextView
        android:id="@+id/highscore_int"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="0dp"
        android:layout_weight="0.14"
        android:text="12312312"
        android:textColor="@android:color/black"
        android:textSize="40sp" />

</LinearLayout>

I can't see the highscore_int on my device, only at the eclipse xml file grahpic layout.

DavidBalas
  • 333
  • 7
  • 21
  • because you have same name for your inner class which is not allowed in java. you need to change `MainActivity` on that line – madteapot Jul 07 '14 at 13:11
  • Why do you call your class MainActivity when it is not an Activity? Why don't you call it MainFragment instead? Your code is confusing! – WarrenFaith Jul 07 '14 at 13:11
  • [See this answer](http://stackoverflow.com/questions/9246246/nested-type-cannot-hide-an-enclosing-type) Hint: MainActivity twice. – RossC Jul 07 '14 at 13:11
  • @RossC May I ask why I can't see the highscore_int textview? – DavidBalas Jul 07 '14 at 13:14
  • @WarrenFaith May I ask why I can't see the highscore_int textview? – DavidBalas Jul 07 '14 at 13:15
  • @Setu May I ask why I can't see the highscore_int textview? – DavidBalas Jul 07 '14 at 13:15
  • @DavidBalas can you explain "Why I can't see the highscore_int textview?". I don't understand what you are saying. – madteapot Jul 07 '14 at 13:18
  • @Setu At the Eclipse xml file when I press Graphical Layout, I can see the "highscore_int" textview, but when I go to my device, it's just a blank page. – DavidBalas Jul 07 '14 at 13:38

1 Answers1

1

You have defined MainActivity twice. Rename the second one to e.g. MainFragment (or something else) and you should be fine. But I would also recommend to move the Fragment to a different file as well.

AKroell
  • 622
  • 4
  • 18