2017-12-20 9 views
0

単純なUIでアプリケーションを作成しようとしています。 1つのアクティビティで構成され、2つのボタン、テキストビュー、および画面の半分を埋め尽くすGoogleマップフラグメントで構成されています。 Android Studioから提供されるドラッグアンドドロップ要素を使用してレイアウトを作成しました。私は、エラーをGoogleで検索しましたし、解決策を試してみました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> 

EDIT:入力用 おかげで、少し微調整した後、私はそれが働いて得ることができました。あなたが活動に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を返します、あなたがでのMapView

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

を追加しました:私は次のように私の活動のレイアウト上のマップを置き換えますレイアウトMapFragmentを使用してください。MapFragmentにMapViewを割り当てる理由は何ですか?

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

ここでは、MapViewのを使用する必要がどこで見ることができるとMapFragment

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

+0

提案をいただき、ありがとうございました。フラグメントがコンテナ内に存在するようにすることは必須ですか(たとえば、相対的なレイアウト)、コンテナなしでフラグメントが存在する可能性はありますか? – dfqupwndguerrillamailde

関連する問題