0

アプリを起動すると、ロゴを表示するスプラッシュスクリーンが表示され、バックグラウンドで GPS 座標 (緯度と経度) が取得されます。その後、ビューページャー、FragmentManager、および MyFragmentPagerAdapter を設定する MainFragmentActivity に移動します。MyFragmentPagerAdapter でフラグメントをセットアップします。それが私の問題です。GPS 座標をフラグメントに取得できないようです。

簡単な要約: GPS 調整はスプラッシュ スクリーンで計算されます。スプラッシュ スクリーンが完了すると、MainFragmentActivity が開き、ビューページャーが機能するようにすべてがセットアップされます。次に、フラグメントのスプラッシュスクリーンで計算された GPS コーディネーションにアクセスできるようにしたいと考えています。それらを余分に渡そうとしましたが、私のフラグメントはintent startActivty. だから私はここで立ち往生しています。

スプラッシュスクリーン

//Calculating GPS coordinations ...

     CountDown tik;
     tik = new CountDown(3000, 1000, this, MainActivity.class);
     tik.start();
     StartAnimations();

秒読み

//After SplashScreen is done, start MainActivity
public class CountDown extends CountDownTimer{
    private Activity myActivity;
    private Class myClass;

    public CountDown(long millisInFuture, long countDownInterval, Activity act, Class cls) {
        super(millisInFuture, countDownInterval);
        myActivity = act;
        myClass = cls;
    }

    @Override
    public void onFinish() {
        myActivity.startActivity(new Intent(myActivity, myClass));
        myActivity.finish();
    }

    @Override
    public void onTick(long millisUntilFinished) {}
}

主な活動

//Setting viewpager up

public class MainActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Tried getting coordinations like this and then passing it to fragment, but didn't succeed.
        /*Bundle extras = getIntent().getExtras();
        myLat = Double.toString(extras.getDouble("lat"));
        myLong = Double.toString(extras.getDouble("lng"));*/

        /** Getting a reference to the ViewPager defined the layout file */
        final ViewPager pager = (ViewPager) findViewById(R.id.pager);

        /** Getting fragment manager */
        FragmentManager fm = getSupportFragmentManager();

        /** Instantiating FragmentPagerAdapter */
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);

        /** Setting the pagerAdapter to the pager object */
        pager.setAdapter(pagerAdapter);

        pager.setPageTransformer(true, new ZoomOutPageTransformer());

        PageListener pagelistener = new PageListener();
        pager.setOnPageChangeListener(pagelistener);
}

MyFragmentPagerAdapter

public class MyFragmentPagerAdapter extends FragmentPagerAdapter{

    final int PAGE_COUNT = 6;

    /** Constructor of the class */
    public MyFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    /** This method will be invoked when a page is requested to create */
    @Override
    public Fragment getItem(int arg0) {

        switch(arg0){

        case 0:

            return new FragmentOne();           

        case 1:
            return new FragmentTwo();

        case 2:
            return new FragmentThree();

        case 3:
            return new FragmentFour();

        case 4:
            return new FragmentFive();

        case 5:
            return new SettingsFragment();          

        default:
            return null;

        }       
    }

    /** Returns the number of pages */
    @Override
    public int getCount() {
        return PAGE_COUNT;
    }    
}

**フラグメントの例。この例では FragmentOne を使用しました

public class FragmentOneextends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        //Want to be able to access those coordinations here.
        View v = inflater.inflate(R.layout.frag_one, container, false);
    }
}

ロケーションヘルパー

    public class LocationHelper extends Activity{
        private static LocationHelper mInstance = new LocationHelper();
        private LatLng mLocation;
        public static double location_latitude;
        public static double location_longitude;
        LocationManager locationManager;
        LocationListener locationListener;

        private LocationHelper() {
        super();
            // start getting location
     // Acquire a reference to the system Location Manager
            locationManager = (LocationManager) this
                    .getSystemService(Context.LOCATION_SERVICE);
    //If I remove 'extends Activity, then I get this error: The method getSystemService(String) is undefined for the type LocationHelper

            // Define a listener that responds to location updates
            locationListener = new LocationListener() {
                public void onLocationChanged(Location location) {
                    // Called when a new location is found by the network location
                    // provider.
                    location_latitude = location.getLatitude();
                    location_longitude = location.getLongitude();

                    Log.v("Location", "set" + location.getLatitude());

                    if (location.getLatitude() != 0.0) {
                        Log.v("Location", "stop looking");
                        locationManager.removeUpdates(locationListener);

                        FileOutputStream fos;
                        try {
//Getting the same error here The method openFileOutput(String, int) is undefined for the type new LocationListener(){}
                            fos = openFileOutput("my_latitude", Context.MODE_PRIVATE);
                            fos.write((""+location_latitude).getBytes());
                            fos.close();

                            fos = openFileOutput("my_longitude", Context.MODE_PRIVATE);
                            fos.write((""+location_longitude).getBytes());
                            fos.close();

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    } else {
                        Log.v("Location", "keep looking");
                    }
                }

                public void onStatusChanged(String provider, int status,
                        Bundle extras) {
                    Log.v("Location", provider + ", " + status + " Status changed");
                }

                public void onProviderEnabled(String provider) {
                    Log.v("Location", provider + " onProviderEnabled");
                }

                public void onProviderDisabled(String provider) {
                    Log.v("Location", provider + " onProviderDisabled");
                }
            };
        }

        public static LocationHelper getInstance() {
            return mInstance;
        }

        public LatLng getLocation() {
            return mLocation;
        }

    }
4

2 に答える 2

1

Intent問題は、アクティビティを開始するために使用している緯度と経度を入力していないことです。次のようなものが必要です。

@Override
public void onFinish() {
    Intent intent = new Intent(myActivity, myClass);
    intent.addExtra("lat", latitude);
    intent.addExtra("lon", longitude);
    myActivity.startActivity(intent);
    myActivity.finish();
}

から にデータを渡すには、を呼び出して、から取得した を渡す必要がありActivityます。このようなもの:FragmentsetArguments(Bundle args)FragmentBundleIntent

myFragment.setArguments(getIntent().getExtras());
于 2013-06-05T14:38:29.727 に答える