2014-12-07 38 views
0

主類谷歌地圖在標籤初始化失敗

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 

    } 
} 

,我用

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具有的fragementadapter地圖片段

<?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?請幫我解決這個

回答

0

看起來你正在擷取的地圖,當你片段的佈局尚未初始化。爲了確認,延遲InitializeMap()

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

如果這樣的作品,那麼我建議你以某種方式等待片段的佈局進行充氣。也許你應該將片段內InitializeMap()的地圖是它的一部分,而不是活動。

+0

好的,謝謝你,我會嘗試這種解決方案,謝謝您的回覆 – 2014-12-09 05:19:15