2017-12-20 2 views
0

간단한 UI로 앱을 만들려고합니다. 하나의 액티비티로 구성되며 2 개의 버튼, 텍스트 뷰 및 화면 절반을 채우는 Google 맵 조각으로 구성됩니다. Android Studio에서 제공하는 드래그 앤 드롭 요소를 사용하여 레이아웃을 만들었습니다. 내가 오류를 검색 좀했는데 해결책을 시도getMapAsync를 사용할 때 null 포인터 가져 오기

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback{ 

RequestQueue requestQueue; 
Button serviceButton; 
TextView textViewService; 
SensorResponse responseJson; 
private GoogleMap googleMap; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestQueue = Volley.newRequestQueue(this); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.mapView2); 
    mapFragment.getMapAsync(this); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    textViewService = findViewById(R.id.textViewServiceOutput); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
        .setAction("Action", null).show(); 
     } 
    }); 
} 

@Override 
public void onMapReady(GoogleMap map) { 

    googleMap = map; 

    setUpMap(); 

} 
public void setUpMap(){ 

    googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
    //googleMap.setMyLocationEnabled(true); 
    googleMap.setTrafficEnabled(true); 
    googleMap.setIndoorEnabled(true); 
    googleMap.setBuildingsEnabled(true); 
    googleMap.getUiSettings().setZoomControlsEnabled(true); 
}} 

: 이것은 내 활동 jjava.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference

:

문제는 내가 그것을 실행하려고하면, 나는 다음과 같은 오류를 얻을 수 있다는 것입니다 SupportMapFragment과 같은 시도를 했는데도 여전히 작동하지 않습니다. 다음

내 레이아웃 파일입니다

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="com.example.tarikh.myapplication.MainActivity" 
tools:showIn="@layout/activity_main"> 

<Button 
    android:id="@+id/button3" 
    android:layout_width="wrap_content" 
    android:layout_height="47dp" 
    android:layout_marginEnd="8dp" 
    android:layout_marginStart="8dp" 
    android:layout_marginTop="8dp" 
    android:onClick="callTheService" 
    android:text="Call Service" 
    app:layout_constraintEnd_toEndOf="parent" 
    app:layout_constraintStart_toStartOf="parent" 
    app:layout_constraintTop_toBottomOf="@+id/mapButton" /> 

<TextView 
    android:id="@+id/textViewServiceOutput" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="128dp" 
    android:layout_marginRight="128dp" 
    android:layout_marginTop="16dp" 
    android:text="Parking Bay Checker" 
    app:layout_constraintEnd_toEndOf="parent" 
    app:layout_constraintTop_toTopOf="parent" /> 

<Button 
    android:id="@+id/mapButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="8dp" 
    android:layout_marginStart="8dp" 
    android:layout_marginTop="8dp" 
    android:onClick="callTheMap" 
    android:text="Call Map" 
    app:layout_constraintEnd_toEndOf="parent" 
    app:layout_constraintStart_toStartOf="parent" 
    app:layout_constraintTop_toBottomOf="@+id/textViewServiceOutput" /> 

<com.google.android.gms.maps.MapView 
    android:id="@+id/mapView2" 
    android:layout_width="match_parent" 
    android:layout_height="244dp" 
    android:layout_marginEnd="8dp" 
    android:layout_marginStart="8dp" 
    android:layout_marginTop="44dp" 
    app:layout_constraintEnd_toEndOf="parent" 
    app:layout_constraintHorizontal_bias="0.0" 
    app:layout_constraintStart_toStartOf="parent" 
    app:layout_constraintTop_toBottomOf="@+id/button3" /> 

</android.support.constraint.ConstraintLayout> 

편집 : 입력 덕분에 조금의 조정 후, 나는 그것이 작동 얻을 관리했습니다. 당신이 활동에 MapFragment가을 추가하지 않은으로 문 아래

<fragment 
    android:id="@+id/mapFrag" 
    class="com.google.android.gms.maps.MapFragment" 
    android:layout_width="match_parent" 
    android:layout_height="329dp" 
    android:layout_marginTop="164dp" 
    app:layout_constraintStart_toStartOf="parent" 
    app:layout_constraintTop_toTopOf="parent" /> 
+0

하면 레이아웃 파일을 업데이트 할 수 사용해야 할 위치를 볼 수 있습니까? – R2R

+0

@ R2R이 레이아웃 파일 – dfqupwndguerrillamailde

답변

1

는, null를 돌려줍니다, 당신이 지도보기

(MapFragment) getFragmentManager().findFragmentById(R.id.mapView2) 
1

을 추가 한 : 나는 다음 내 활동 레이아웃에지도를 교체 레이아웃 MapFragment을 사용하려고합니다. MapFragment에 MapView를 지정하면 null이됩니다. 원인은 다음과 같습니다.

여기

우리가지도보기 및 MapFragment가

https://stackoverflow.com/a/34626302/3505534

+0

과 함께 질문을 업데이트했습니다. 제안 해 주셔서 감사합니다. 조각을 컨테이너 (예 : 상대 레이아웃) 내에 두는 것이 의무입니까, 아니면 조각이 컨테이너가없는 상태로 존재할 수 있습니까? – dfqupwndguerrillamailde

관련 문제