选项卡初始化中的Google Map失败

时间:2014-12-07 06:26:06

标签: java android google-maps tabs

主要班级

package com.android.takemethere;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.TargetApi;
import android.app.ActionBar;

import com.android.takemethere.backend.ClassLogin;
import com.android.takemethere.backend.Database_GetAllReports;
import com.android.takemethere.objects.*;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.wearable.internal.ac;

import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;


public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

    private ActionBar actionbar;
    private ViewPager viewpager;
    private FragmentPageAdapter ft;
    private Resources res;
    private ActionBar bar;


    private GoogleMap googleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabhome);






        bar = getActionBar();
        getActionBar().setDisplayHomeAsUpEnabled(true);
        bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FE2E2E")));

        viewpager=(ViewPager) findViewById(R.id.pager);
        ft=new FragmentPageAdapter(getSupportFragmentManager());
        InitalizeMap();
        actionbar=getActionBar();
        viewpager.setAdapter(ft);
        actionbar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FE2E2E")));
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionbar.addTab(actionbar.newTab().setTabListener(this)
                        .setIcon(R.drawable.ic_home));

        actionbar.addTab(actionbar.newTab().setTabListener(this)
                .setIcon(R.drawable.ic_newsfeeds));
        actionbar.addTab(actionbar.newTab().setText("Profile").setTabListener(this));
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                actionbar.setSelectedNavigationItem(arg0);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });

      ///  InitalizeMap();
     //  
    }

    public void InitalizeMap() {
           if (googleMap == null) {
                googleMap = ((SupportMapFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.map)).getMap();

                // check if map is created successfully or not
                if (googleMap == null) {
                    Toast.makeText(getApplicationContext(),
                            "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                            .show();
                }
            }


       }






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


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent SwitchActivity;
         switch (item.getItemId()) {
         case R.id.action_search:
                 SwitchActivity= new Intent(getApplicationContext(),class_search.class);
                startActivity(SwitchActivity);
             return true;

         case R.id.action_report:
                SwitchActivity= new Intent(getApplicationContext(),Class_report.class);
                startActivity(SwitchActivity);  
             return true;
         default:
             return super.onOptionsItemSelected(item);
         }
    }


    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }


    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

        viewpager.setCurrentItem(tab.getPosition());


    }


    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }
}

我使用的fragementadapter

package com.android.takemethere.objects;

import com.android.takemethere.*;


import android.app.ActionBar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class FragmentPageAdapter extends FragmentPagerAdapter {

    public FragmentPageAdapter(FragmentManager fm) {    
        super(fm);
        // TODO Auto-generated constructor stub

    }

    @Override
    public Fragment getItem(int arg0) {
        // TODO Auto-generated method stub
        switch(arg0){
        case 0:

            return new class_home();
        case 1:

            return new class_newsfeeds();
        case 2:

            return new class_profile();
        }
        return null;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 3;
    }

}

包含标签

的tab_home.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   >

</android.support.v4.view.ViewPager>

具有地图片段

的layout_home.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center|left"
        android:orientation="vertical" >

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical" >



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

    </LinearLayout>

</LinearLayout>

class_home.java

package com.android.takemethere;



import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.android.takemethere.maps.direction.*;

import android.app.ActionBar;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

public class class_home extends Fragment {
    private static View view;
    GoogleMap googleMap;


    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState){ 
        if (view != null) {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null)
                parent.removeView(view);
        }
        try {
            view = inflater.inflate(R.layout.layout_home, container, false);
        } catch (InflateException e) {
            /* map is already there, just return view as it is */
        }



        return view;
    }





}

错误:

12-07 14:07:48.973: E/AndroidRuntime(23264): Caused by: java.lang.NullPointerException
12-07 14:07:48.973: E/AndroidRuntime(23264):    at    com.android.takemethere.MainActivity.InitalizeMap(MainActivity.java:107)

我有这个错误它看起来在运行时找不到地图ID?请帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

当您的片段布局尚未初始化时,您似乎正在抓取地图。要确认这一点,请延迟InitializeMap()

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        InitalizeMap();
    }
}, 3000); // delayed by 3 seconds

如果有效,那么我建议你以某种方式等待片段的布局膨胀。也许你应该在你的片段里InitializeMap(),因为地图是它的一部分,而不是活动。