Autore Topic: Map View e PageView  (Letto 671 volte)

Offline m2k

  • Nuovo arrivato
  • *
  • Post: 18
  • Respect: 0
    • Mostra profilo
    • KyTech
  • Dispositivo Android:
    Samsung Galaxy SII
  • Sistema operativo:
    Windows 7 - Ubuntu - Debian
Map View e PageView
« il: 27 Settembre 2012, 17:30:41 CEST »
0
Salve a tutti,
Oggi avendo un po di tempo ho deciso di dedicarlo alla programmazione android.
Premetto che dal ultimo post che ho fatto non ho imparato nulla in piu, quindi le mie conoscenze sono ancora limitate!  ;-)
Quindi chiedo a voi aiuto per questo problema.
Usando il tutorial di Nicola_D ho creato due "Pagine".
In una di esse vorrei poter integrare una mappa. Purtroppo il tutorial presente in questo forum non mi è stato di aiuto, quindi ho cercato altrove.
Ho realizzato questa activity:
Codice (Java): [Seleziona]
package it.kytech.findfun;

import android.support.v4.app.Fragment;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;

public class MapActivity extends Fragment {

    private MapView map=null;
    private MyLocationOverlay me=null;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // fragment not when container null
        if (container == null) {
            return null;
        }
        // inflate view from layout
        View view = (LinearLayout) inflater.inflate(R.layout.page2, container, false);
        // Otteniamo il riferimento alla MapView        

        return view;
    }
   
     @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
   
    map=new MapView(getActivity(), "0D4xtBjYDBdTPH-2OXJU7vQ2LjXsdYDvbWN2d5w");
    map.setClickable(true);
 
    map.getController().setCenter(getPoint(40.76793169992044,
                                            -73.98180484771729));
    map.getController().setZoom(17);
    map.setBuiltInZoomControls(true);
   
    Drawable marker=getResources().getDrawable(R.drawable.marker);
   
    marker.setBounds(0, 0, marker.getIntrinsicWidth(),
                            marker.getIntrinsicHeight());
   
    map.getOverlays().add(new SitesOverlay(marker));
   
    me=new MyLocationOverlay(getActivity(), map);
    map.getOverlays().add(me);
   
    ((ViewGroup)getView()).addView(map);
  }
 
  @Override
  public void onResume() {
    super.onResume();
   
    me.enableCompass();
  }
 
  @Override
  public void onPause() {
    super.onPause();
   
    me.disableCompass();
  }

  private GeoPoint getPoint(double lat, double lon) {
    return(new GeoPoint((int)(lat*1000000.0),
                          (int)(lon*1000000.0)));
  }
   
  private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
    private List<OverlayItem> items=new ArrayList<OverlayItem>();
   
    public SitesOverlay(Drawable marker) {
      super(marker);
     
      boundCenterBottom(marker);
     
      items.add(new OverlayItem(getPoint(40.748963847316034,
                                          -73.96807193756104),
                                "UN", "United Nations"));
      items.add(new OverlayItem(getPoint(40.76866299974387,
                                          -73.98268461227417),
                                "Lincoln Center",
                                "Home of Jazz at Lincoln Center"));
      items.add(new OverlayItem(getPoint(40.765136435316755,
                                          -73.97989511489868),
                                "Carnegie Hall",
              "Where you go with practice, practice, practice"));
      items.add(new OverlayItem(getPoint(40.70686417491799,
                                          -74.01572942733765),
                                "The Downtown Club",
                        "Original home of the Heisman Trophy"));

      populate();
    }
   
    @Override
    protected OverlayItem createItem(int i) {
      return(items.get(i));
    }
   
    @Override
    protected boolean onTap(int i) {
      Toast.makeText(getActivity(),
                      items.get(i).getSnippet(),
                      Toast.LENGTH_SHORT).show();
     
      return(true);
    }
   
    @Override
    public int size() {
      return(items.size());
    }
  }

}

Mentre l'activity principale è
Codice (Java): [Seleziona]
package it.kytech.findfun;


import java.util.List;
import java.util.Vector;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.view.View;
import com.google.android.maps.MapActivity;

public class PagerTabsActivity extends FragmentActivity  {
        // list contains fragments to instantiate in the viewpager
        List<Fragment> fragments = new Vector<Fragment>();
        List<String> fragmentTitles = new Vector<String>();
        // page adapter between fragment list and view pager
        private PagerAdapter mPagerAdapter;
        // view pager
        private ViewPager mViewPager;

        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                // creating fragments and adding to list
                fragments.add(Fragment.instantiate(this, ListActivity.class.getName()));
                fragmentTitles.add(""+R.string.listactivity_name);
                fragments.add(Fragment.instantiate(this, MapActivity.class.getName()));
                fragmentTitles.add(""+R.string.mapactivity_name);
               
                mViewPager = (ViewPager) findViewById(R.id.pager);
                View pagerStrip = findViewById(R.id.pagerTabStrip);
                if (pagerStrip instanceof PagerTabStrip) {
                        PagerTabStrip pagerTabStrip = (PagerTabStrip) pagerStrip;
                        pagerTabStrip.setDrawFullUnderline(true);
                        pagerTabStrip.setTabIndicatorColorResource(android.R.color.holo_blue_light);
                        // pagerTabStrip.setTextColor(color.white);
                }

                this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments, fragmentTitles);
                mViewPager.setAdapter(this.mPagerAdapter);
                mViewPager.setCurrentItem(0);
        }

}

Gli errori che ottengo sono:

15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime   FATAL EXCEPTION: main
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime   java.lang.RuntimeException: Unable to start activity ComponentInfo{it.kytech.findfun/it.kytech.findfun.PagerTabsActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.maps.MapActivity: make sure class name exists, is public, and has an empty constructor that is public
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime       at android.app.ActivityThread.access$600(ActivityThread.java:122)
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime       at android.os.Handler.dispatchMessage(Handler.java:99)
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime       at android.os.Looper.loop(Looper.java:137)
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime       at android.app.ActivityThread.main(ActivityThread.java:4340)
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime       at java.lang.reflect.Method.invokeNative(Native Method)
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime       at java.lang.reflect.Method.invoke(Method.java:511)
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime       at dalvik.system.NativeStart.main(Native Method)
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime   Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.maps.MapActivity: make sure class name exists, is public, and has an empty constructor that is public
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime       at android.support.v4.app.Fragment.instantiate(Fragment.java:399)
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime       at android.support.v4.app.Fragment.instantiate(Fragment.java:363)
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime       at it.kytech.findfun.PagerTabsActivity.onCreate(PagerTabsActivity.java:31)
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime       at android.app.Activity.performCreate(Activity.java:4465)
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime       ... 11 more
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime   Caused by: java.lang.InstantiationException: can't instantiate class com.google.android.maps.MapActivity
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime       at java.lang.Class.newInstanceImpl(Native Method)
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime       at java.lang.Class.newInstance(Class.java:1319)
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime       at android.support.v4.app.Fragment.instantiate(Fragment.java:388)
15:27:35.053   762   it.kytech.findfun   ERROR   AndroidRuntime       ... 16 more


Ovviamente l'applicazione viene terminata.
Ora non essendo esperto vorrei capire quale è stato il mio errore e se con il vostro contributo postrei risolverlo.

Ringrazio in anticipo coloro che mi aiuteranno.