-2

我有這個地圖活動,我想在其中創建導航欄/抽屜。 有一個在MapsActivity一個錯誤說 「無法解析方法setSupportActionBar(android.support.v7.widget.Toolbar)」在地圖活動上創建導航欄Android

MapsActivity.java

public class MapsActivity extends FragmentActivity implements 
OnMapReadyCallback { 

String [] places= {"bais city","basay","bindoy"}; 
AutoCompleteTextView textview; 

private GoogleMap mMap; 
private LatLng latLng; 
private Marker marker; 

private Toolbar category; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 
    // Obtain the SupportMapFragment and get notified when the map is ready 
     to be used. 
    SupportMapFragment mapFragment = (SupportMapFragment) 
     getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 

    category = (Toolbar) findViewById(R.id.Navbar); 
    setSupportActionBar(category); //i got my error here says cannot resolve 
    method 

} 

進口

import android.content.DialogInterface; 
import android.location.Address; 
import android.location.Geocoder; 
import android.preference.DialogPreference; 
import android.support.v4.app.FragmentActivity; 
import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.support.v7.app.AlertDialog; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.AutoCompleteTextView; 
import android.widget.EditText; 
import android.widget.TextView; 

import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.LatLngBounds; 
import com.google.android.gms.maps.model.Marker; 
import com.google.android.gms.maps.model.MarkerOptions; 

import java.io.IOException; 
import java.util.List; 
import java.util.Locale; 

地圖活動佈局( xml)

<!--suppress ALL --> 
<LinearLayout xmlns:map="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical"> 
<TextView 
    android:text="@string/test" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/PSString" 
    android:textSize="22sp"/> 


<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <include layout="@layout/navigation" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 


    <AutoCompleteTextView 
     android:layout_width="278dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/autoCompleteTextView" 
     tools:ignore="LabelFor" /> 

    <Button 
     android:text="@string/search" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/BTNSearch" 
     android:layout_gravity="end" 
     android:onClick="onSearch" /> 

    <fragment 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.SupportMapFragment" 
     android:layout_width="384dp" 
     android:layout_height="554dp" 
     tools:context="com.example.boneyflesh.homepage.MapsActivity" 
     tools:layout="@layout/activity_main" /> 
    </LinearLayout> 



</LinearLayout> 

導航欄佈局(xml)

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar  
xmlns:android="http://schemas.android.com/apk/res/android" 

android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:id="@+id/Navbar"> 

</android.support.v7.widget.Toolbar> 

MainActivity

public class MainActivity extends AppCompatActivity { 

public void whatodo(View view){ 
    if(view.getId() == R.id.BTNStart){ 
     AlertDialog.Builder prompt = new 
     AlertDialog.Builder(MainActivity.this); 
     prompt.setCancelable(false) 
       .setPositiveButton("Documents", new 
        DialogInterface.OnClickListener() { 
          @Override 
          public void onClick(DialogInterface dialog, int 
          which) { 

          } 
         } 
       ) 
        .setNegativeButton("2D MAP", new 
        DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int 
         which) { 
          startActivity(new 
         Intent(getApplicationContext(),MapsActivity.class)); 
         } 
        } 
        ); 


     AlertDialog alert = prompt.create(); 
     alert.setTitle("Please select an option"); 
     alert.show(); 

    } 
    if(view.getId() == R.id.BTNInfo){ 

    } 
    if(view.getId() == R.id.BTNClose){ 
     finish(); 
     System.exit(0); 
    } 
} 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

} 

}

回答

0

它無法解決setSupportActionBar()方法,因爲這種方法不FragmentActivity實現。理想情況下,您應該從AppCompatActivity繼承您的活動,這將允許您使用工具欄以及碎片。

+0

我編輯了我的帖子,我對這個東西真的很陌生,我該如何做到這一點與我在MainActivity上的設置? 我設置了「我的警報」對話框來打開地圖活動。 – Boneyflesh

+0

...而不是'擴展FragmentActivity',它應該是'擴展AppCompatActivity'。 –

+0

謝謝,錯誤消失了,但我仍然在如何繪製導航欄掙扎,生病了一點點修補。 :豎起大拇指: – Boneyflesh