0

我尝试调用包含来自 Google 地图的地图的 FragmentActivity ......但日志显示了这个错误:

08-13 20:35:02.764: E/AndroidRuntime(575): FATAL EXCEPTION: main
08-13 20:35:02.764: E/AndroidRuntime(575): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testeslidingmenuagvdnc/com.netimoveis.appandroid.Menu_ImoveisNoMapa}: java.lang.NullPointerException
08-13 20:35:02.764: E/AndroidRuntime(575):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
08-13 20:35:02.764: E/AndroidRuntime(575):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
08-13 20:35:02.764: E/AndroidRuntime(575):  at android.app.ActivityThread.access$1500(ActivityThread.java:123)
08-13 20:35:02.764: E/AndroidRuntime(575):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
08-13 20:35:02.764: E/AndroidRuntime(575):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-13 20:35:02.764: E/AndroidRuntime(575):  at android.os.Looper.loop(Looper.java:126)
08-13 20:35:02.764: E/AndroidRuntime(575):  at android.app.ActivityThread.main(ActivityThread.java:3997)
08-13 20:35:02.764: E/AndroidRuntime(575):  at java.lang.reflect.Method.invokeNative(Native Method)
08-13 20:35:02.764: E/AndroidRuntime(575):  at java.lang.reflect.Method.invoke(Method.java:491)
08-13 20:35:02.764: E/AndroidRuntime(575):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
08-13 20:35:02.764: E/AndroidRuntime(575):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
08-13 20:35:02.764: E/AndroidRuntime(575):  at dalvik.system.NativeStart.main(Native Method)
08-13 20:35:02.764: E/AndroidRuntime(575): Caused by: java.lang.NullPointerException
08-13 20:35:02.764: E/AndroidRuntime(575):  at com.netimoveis.appandroid.Menu_ImoveisNoMapa.onCreate(Menu_ImoveisNoMapa.java:29)
08-13 20:35:02.764: E/AndroidRuntime(575):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
08-13 20:35:02.764: E/AndroidRuntime(575):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
08-13 20:35:02.764: E/AndroidRuntime(575):  ... 11 more

调用 googleMaps 的片段活动:

public class Menu_ImoveisNoMapa extends android.support.v4.app.FragmentActivity {

    private GoogleMap map;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu__imoveis_no_mapa);

        LatLng latLong = new LatLng(-2033681,-4028611);
        LatLng latLong2 = new LatLng(-2354356,-4663941);

        //map = ((MapFragment) (getFragmentManager().findFragmentById(R.id.map))).getMap(); 
        map = ((MapFragment) (getFragmentManager().findFragmentById(R.id.map))).getMap();
        //map.addMarker(new MarkerOptions().position(latLong).title("Exemplo1"));
        //map.addMarker(new MarkerOptions().position(latLong2).title("Exemplo2"));

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu__imoveis_no_mapa, menu);
        return true;
    }

}

///////////////////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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Menu_ImoveisNoMapa" >


<fragment 
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.SupportMapFragment"/>


</RelativeLayout>

我在这里称之为 FragmentActivity(案例 101):

public class SlidingMenuFragment extends Fragment implements ExpandableListView.OnChildClickListener {

    private ExpandableListView sectionListView;

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

        List<Section> sectionList = createMenu();

        View view = inflater.inflate(R.layout.slidingmenu_fragment, container,false);
        this.sectionListView = (ExpandableListView) view.findViewById(R.id.slidingmenu_view);


         ImageView tv = new ImageView(getActivity());
         Drawable dw = getResources().getDrawable(R.drawable.menu_netimoveis_locacao);
         tv.setImageDrawable(dw);
         this.sectionListView.addHeaderView(tv);


        this.sectionListView.setGroupIndicator(null);

        SectionListAdapter sectionListAdapter = new SectionListAdapter(this.getActivity(), sectionList);
        this.sectionListView.setAdapter(sectionListAdapter); 

        this.sectionListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
              @Override
              public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true;
              }
            });

        this.sectionListView.setOnChildClickListener(this);

        int count = sectionListAdapter.getGroupCount();
        for (int position = 0; position < count; position++) {
            this.sectionListView.expandGroup(position);
        }

        return view;
    }

    private List<Section> createMenu() {
        List<Section> sectionList = new ArrayList<Section>();

        Section oDemoSection = new Section("Menu");
        oDemoSection.addSectionItem(101,"Imoveis no Mapa ", "map_marker");
        oDemoSection.addSectionItem(102, "Favoritos ", "star");
        oDemoSection.addSectionItem(103, "Agências ", "house");
        oDemoSection.addSectionItem(104, "Dicas para alugar ", "coffee");
        oDemoSection.addSectionItem(105, "Alugar meu imóvel ", "pricetag");

//        
//        Section oGeneralSection = new Section("General");
//        oGeneralSection.addSectionItem(201, "Settings", "slidingmenu_settings");
//        oGeneralSection.addSectionItem(202, "Rate this app", "slidingmenu_rating");
//        oGeneralSection.addSectionItem(203, "Eula", "slidingmenu_eula");
//        oGeneralSection.addSectionItem(204, "Quit", "slidingmenu_quit");
//        
        sectionList.add(oDemoSection);
        //sectionList.add(oGeneralSection);
        return sectionList;
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
        FragmentActivity newContent = null;
        switch ((int)id) {
        case 101:
            Intent myIntent = new Intent(getActivity(), Menu_ImoveisNoMapa.class);
            getActivity().startActivity(myIntent); 
            break;
        case 102:
            //TODO
            break;
        case 201:
            //TODO
            break;
        case 202:
            //TODO
            break;
        case 203:
            //TODO
            break;
        case 204:
            //TODO
            break;
        }

        return false;
    }

}
4

0 回答 0