2017-04-23 138 views
0

我有一個main_activity和一個底部欄。我正在使用此底部欄來瀏覽片段。但是,有一個片段不能通過底部欄直接導航。它必須可以從MapView和onInfoWindowClick的片段中訪問。但是,每當我使用infoWindow時,應用程序崩潰。這是我的代碼使用MapView切換片段

public void onMapReady(GoogleMap googleMap) { 
     BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.drawable.tesla); 
     mMap=googleMap; 
     for (Location item : array_list) { 
      String _title = item.getLocation_name(); 
      String _snippet = item.getLocation_address(); 
      LatLng _latLng = new LatLng(item.getLatitude(), item.getLongitude()); 
      final MarkerOptions mMarkerOptions = new MarkerOptions().icon(icon); 
      mMarkerOptions.position(_latLng).title(_title).snippet(_snippet).flat(true); 
      Marker marker = mMap.addMarker(mMarkerOptions); 
      mMarkerHashMap.put(marker, item); 

      CameraPosition cameraPosition = new CameraPosition.Builder() 
        .target(marker.getPosition()) 
        .zoom(12.0f) 
        .build(); 
      mMap.animateCamera(CameraUpdateFactory 
        .newCameraPosition(cameraPosition)); 

      mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() { 
       @Override 
       public void onInfoWindowClick(Marker marker) { 
        LocationDetailFragment detailedlocation = new LocationDetailFragment(); 

        detailedlocation.LOCATION = mMarkerHashMap.get(marker); 
        FragmentTransaction ft = getFragmentManager().beginTransaction(); 
        ft.replace(R.id.content, detailedlocation); 
        ft.addToBackStack(null); 
        ft.commit(); 
       } 
      }); 
     } 
                   --------- beginning of crash 04-23 19:41:11.380 2573-2573/com.teslaqubitsins.fasih.teslahcm E/AndroidRuntime: FATAL 

例外:主 工藝:com.teslaqubitsins.fasih.teslahcm,PID:2573 顯示java.lang.NullPointerException:嘗試調用虛擬方法 「android.view。查看null上的android.view.View.findViewById(int)' object reference at com.teslaqubitsins.fasih.teslahcm.LocationDetailFragment.onCreateView(LocationDetailFragment.java:35) 在 android.support.v4.app.Fragment.performCreateView(Fragment.java:2192) 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299) 在 android.support.v4。 app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528) 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1595) 在 android.support.v4.app.BackStackRecord.executeOps(BackStackRecord。 java:758) at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363) 在 android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149) 在 android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103) 在 android.support.v4 .app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013) at android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:710) at android.os.Handler.handleCallback(Handler.java:751 ) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6119) 在java.lang.reflect.Method.invoke(本機方法) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit。 java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)04-23 19:41:11.383 1564-1957/system_process W/ActivityManager:Force 整理活動com.teslaqubitsins。 fasih.teslahcm/.MainActivity 04-23 19:41:11.414 1296-1653 /? E/SurfaceFlinger的:ro.sf.lcd_density 必須定義爲一個構建屬性

           [ 04-23 19:41:11.456 1564: 3367 D/   ] 
              HostConnection::get() New Host Connection established 0x87cb5680, tid 3367 04-23 

19:41:11.457 1296年至1653年/? d/gralloc_ranchu:gralloc_alloc:格式1和 使用0x900意味着主機顏色緩衝器的創作04-23 19:41:11.457 1564-3367/system_process I/OpenGLRenderer:初始化EGL,版本1.4 19 04-23:41:11.458 1564-3367/system_process D/OpenGLRenderer:交換行爲1 04-23 19:41:11。458 1564-3367/system_process W/OpenGLRenderer:無法選擇配置與 EGL_SWAP_BEHAVIOR_PRESERVED,不重試... 04-23 19:41:11.458 1564-3367/system_process d/OpenGLRenderer:交換行爲0 04-23 19 :41:11.462 1296-1653 /? D/gralloc_ranchu:gralloc_alloc:format 1和 用法0x900意味着創建主機顏色緩衝區04-23 19:41:11.476 1296-1653 /? d/gralloc_ranchu:gralloc_alloc:格式1和用法0x900 意味着主機顏色緩衝器的創作04-23 19:41:11.892 1564年至1591年/ system_process W/ActivityManager:用於 ActivityRecord {34a233 U0 com.teslaqubitsins活動暫停超時。 fasih.teslahcm/.MainActivity t141 f} 04-23 19:41:11.921 1296-1962 /? D/gralloc_ranchu:gralloc_alloc:format 1和 用法0x900意味着創建主機顏色緩衝區04-23 19:41:11.940 1296-1962 /? D/gralloc_ranchu:gralloc_alloc:格式1和用法0x900 意味着創建主機顏色緩衝區04-23 19:41:11.951 1296-1962 /? D/gralloc_ranchu:gralloc_alloc:格式1和用法0x900暗示 創建主機顏色緩衝區04-23 19:41:11.968 1296-1426 /? D/PermissionCache:檢查android.permission.READ_FRAME_BUFFER是否爲 uid = 1000 =>授予(252 us)04-23 19:41:12.025 1296-1426 /? d/gralloc_ranchu:gralloc_alloc:格式1和用法0x333暗示 創建主機顏色緩衝器的 塊引用

package com.teslaqubitsins.fasih.teslahcm; 


import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.support.v4.app.DialogFragment; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 



public class LocationDetailFragment extends Fragment { 
    public static Location LOCATION; 
    TextView _campusName; 
    TextView _campusAddress; 


    public LocationDetailFragment() { 
    } 


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

     View view=inflater.inflate(R.layout.fragment_location_detail, container, false); 

     _campusName = (TextView) getView().findViewById(R.id.campus_name); 
     _campusAddress = (TextView) getView().findViewById(R.id.campus_address); 

     _campusName.setText(LOCATION.getLocation_name()); 
     _campusAddress.setText(LOCATION.getLocation_address()); 
     return view; 

    } 
    public void onClick_call(View view) { 
     dialPhoneNumber(LOCATION.getLocation_phone()); 
    } 
    public void dialPhoneNumber(String phoneNumber) { 
     Intent intent = new Intent(Intent.ACTION_DIAL); 
     intent.setData(Uri.parse("tel:" + phoneNumber)); 
     if (intent.resolveActivity(getActivity().getPackageManager()) != null) { 
      startActivity(intent); 
     } 
    } 
} 

04-23 19:58:11.735 6856-6856/com.teslaqubitsins.fasih。 teslahcm E/AndroidRuntime:致命異常:主 工藝:com.teslaqubitsins.fasih.teslahcm,PID:6856 顯示java.lang.NullPointerException:嘗試調用虛擬方法 「android.view.View android.view.View.findViewById (int)'爲空 對象引用 在 com.teslaqubitsins.fasih.teslahcm.LocationDetailFragment.onCreateView(LocationDetailFragment.java:35) 在 android.support.v4.app.Fragment.performCreateView(Fragment.java:2192) 在 android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1299) 在 android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1528) 在 android.support.v4.app。 FragmentManagerImpl.moveToState(FragmentManager.java:1595) android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:758) 在 android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2363) 在 android.support.v4 .app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2149) 在 android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103) 在 android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager .java:2013) at android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:710 ) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler。java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method ) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)04-23 19: 58:12.507 2006-2282/com.google.android.gms.persistent W/GCoreFlp: 無法返回getLastLocation()04-23 19:58:42.534 2134-2214/com.google.android.googlequicksearchbox:搜索 W/GmsLocationProvider:刪除位置更新時出錯:16

+0

發佈崩潰日誌.... – rafsanahmad007

+0

@ rafsanahmad007。請檢查。 – fasih

+0

也發佈您的'LocationDetailFragment' ..因此異常重點在該類 – rafsanahmad007

回答

1

相反的:

_campusName = (TextView) getView().findViewById(R.id.campus_name); 
_campusAddress = (TextView) getView().findViewById(R.id.campus_address); 

使用片段的膨脹的視圖來獲取的ID:

_campusName = (TextView) view.findViewById(R.id.campus_name); 
_campusAddress = (TextView) view.findViewById(R.id.campus_address); 

出現異常,因爲你getView()返回NULL,它不與該片的View相關。在fragment您必須始終使用充氣視圖的參考。

+0

謝謝:)它的工作。 – fasih

+0

http://stackoverflow.com/questions/43574251/cant-start-a-call-from-within-a-fragment-class – fasih

+0

請在這裏看看。 http://stackoverflow.com/questions/43585343/does-scrollview-work-in-fragments – fasih