如何将按钮放在浮动按钮中?

时间:2016-09-01 21:38:01

标签: java android google-maps android-maps-v2

我似乎不知道如何去做。一旦我点击浮动按钮,我的应用程序崩溃,我没有在我的清单中声明SecondActivity.java

这是activity_maps.xml

     <?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"

        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MapsActivity">



        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:map="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MapsActivity"
            tools:layout="@android:layout/simple_spinner_dropdown_item" />


        <ImageButton
            android:layout_width="65dp"
            android:layout_height="65dp"
            android:id="@+id/imagebutton"
            android:src="@drawable/ic_pin_drop_black_24dp"
            />
        </RelativeLayout>

MapsActivity.java

     import android.app.Activity;
        import android.content.Intent;
        import android.support.v4.app.FragmentActivity;
        import android.os.Bundle;
        import android.support.v7.app.ActionBarActivity;
        import android.view.View;
        import android.widget.Button;
        import android.widget.ImageButton;
        import android.widget.TextView;
        import android.widget.Toast;
        import android.widget.ZoomButton;

        import com.google.android.gms.maps.CameraUpdateFactory;
        import com.google.android.gms.maps.GoogleMap;
        import com.google.android.gms.maps.MapFragment;
          import com.google.android.gms.maps.OnMapReadyCallback;
          import com.google.android.gms.maps.SupportMapFragment;
          import android.app.AppOpsManager;


          import com.google.android.gms.maps.model.BitmapDescriptorFactory;
          import com.google.android.gms.maps.model.LatLng;
          import com.google.android.gms.maps.model.MarkerOptions;
          import com.google.android.gms.appindexing.Action;
          import com.google.android.gms.appindexing.AppIndex;
          import com.google.android.gms.common.api.GoogleApiClient;


          public class MapsActivity extends FragmentActivity implements   OnMapReadyCallback {

          private GoogleMap mMap;
          ImageButton floatButton;

          @Override
          protected void onCreate(Bundle savedinstancestate) {
          super.onCreate(savedinstancestate);
          setContentView(R.layout.activity_maps);


        floatButton = (ImageButton) findViewById(R.id.imagebutton);
        floatButton.setOnClickListener(new View.OnClickListener() {
        @Override
            public void onClick(View view) {
                  Intent i = new   Intent(MapsActivity.this,SecondActivity.class);
                    startActivity(i);
        }
        });

        // 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);
        }




             /**
             * Manipulates the map once available.
            * This callback is triggered when the map is ready to be used.
            * This is where we can add markers or lines, add listeners or move the camera. In this case,
            * we just add a marker near Sydney, Australia.
            * If Google Play services is not installed on the device, the user will be prompted to install
            * it inside the SupportMapFragment. This method will only be triggered once the user has
           * installed Google Play services and returned to the app.
             */
             @Override
            public void onMapReady(GoogleMap googleMap) {
            mMap = googleMap;
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng (6.5084, 3.3842), 14));
           }
         }

我创建了第二个活动         activty_second.xml

           <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout        xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical" android:layout_width="match_parent"
          android:layout_height="match_parent">

        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:map="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MapsActivity"
            tools:layout="@android:layout/simple_spinner_dropdown_item" />

          <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="Lagos Mainland"
            android:padding="8dp"
            android:background="#53fed0"
            android:id="@+id/buttonMaps"
            android:layout_marginLeft="90dp"
            android:layout_marginStart="90dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            />

         <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hospital"
            android:id="@+id/buttonMaps2"
            android:layout_marginTop="35dp"
            android:layout_below="@+id/buttonMaps"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:clickable="true"
            android:background="@color/colorAccent" />

         <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Bus Stops"
            android:id="@+id/buttonMaps3"
            android:clickable="true"
            android:layout_marginTop="35dp"
            android:background="@color/colorAccent"
            android:layout_below="@+id/buttonMaps2"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
         <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Taxi"
            android:id="@+id/buttonMaps4"
            android:layout_below="@+id/buttonMaps3"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="35dp"
            android:clickable="true"
            android:background="@color/colorAccent" />


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Gas Station"
            android:id="@+id/buttonMaps5"
            android:layout_marginTop="35dp"
            android:clickable="true"
            android:background="@color/colorAccent"
            android:layout_below="@+id/buttonMaps4"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
           <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Park"
            android:id="@+id/buttonMaps6"
            android:layout_marginTop="35dp"
            android:clickable="true"
            android:background="@color/colorAccent"
            android:layout_below="@+id/buttonMaps5"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />

             </LinearLayout>

SecondActivity.java

    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageButton;

    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.BitmapDescriptorFactory;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.MarkerOptions;


        public class SecondActivity extends FragmentActivity implements OnMapReadyCallback {
         private GoogleMap mMap;

         @Override
       protected void onCreate(Bundle savedinstancestate) {
        super.onCreate(savedinstancestate);
        setContentView(R.layout.activty_second);

    final Button button1 = (Button) findViewById(R.id.buttonMaps);
    final Button button2 = (Button) findViewById(R.id.buttonMaps2);
    final Button button3 = (Button) findViewById(R.id.buttonMaps3);
    final Button button4 = (Button) findViewById(R.id.buttonMaps4);
    final Button button5 = (Button) findViewById(R.id.buttonMaps5);
    final Button button6 = (Button) findViewById(R.id.buttonMaps6);
    ImageButton floatButton;
    floatButton = (ImageButton) findViewById(R.id.imagebutton);
    floatButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            button1.isShown();
            button2.isShown();
            button3.isShown();
            button4.isShown();
            button5.isShown();
            button6.isShown();


         button1.setOnClickListener(new View.OnClickListener() {
             @Override
            public void onClick(View view) {

                mMap.clear();
                if (button1.isShown()) {

                    mMap.addMarker(new MarkerOptions().position(new LatLng()).title("").snippet("My Location").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher)));

       }
      }

     });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mMap.clear();
                if (button2.isShown()) {

                    mMap.addMarker(new MarkerOptions().position(new LatLng()).title("").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher_hospital)));
       mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(6.5084, 3.3842), 14));
                }


            }

        });
    button3.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View v) {


            mMap.clear();
            if (button3.isShown()) {

                mMap.addMarker(new MarkerOptions().position(new LatLng()).title("").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher_busstop)));


            }

        }

    });
    button4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            mMap.clear();
            if (button4.isShown()) {

                mMap.addMarker(new MarkerOptions().position(new LatLng()).title("").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher_taxi)));

                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(6.5084, 3.3842), 14));
            }
        }

    });

    button5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            mMap.clear();
            if (button5.isShown()) {

                mMap.addMarker(new MarkerOptions().position(new LatLng()).title("").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher_gas)));


            }
        }

     });
     button6.setOnClickListener(new View.OnClickListener() {
         @Override
          public void onClick(View view) {

             mMap.clear();
             if (button6.isShown()) {

                 mMap.addMarker(new MarkerOptions().position(new LatLng()).title("").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher_park)));


             }
        }

    });


     }

 });



      // 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);
 }

我的Android Manifest

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

        package="com.example.android.multidex.myapplication">
        android:theme="@android:style/AppTheme"
         <uses-permission android:name="android.permission.INTERNET"/>
         <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
         <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
         <uses-permission    android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>


        <!--
             The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
             Google Maps Android API v2, but you must specify either coarse or fine
             location permissions for the 'MyLocation' functionality. 
        -->
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"    />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"   />

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="Android Map"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            android:name="android.support.multidex.MultiDexApplication">
            <!--
                 The API key for Google Maps-based APIs is defined as a string resource.
                 (See the file "res/values/google_maps_api.xml").
                 Note that the API key is linked to the encryption key used to sign the APK.
                 You need a different API key for each encryption key, including the release key that is used to
                 sign the APK for publishing.
                 You can define the keys for the debug and release targets in src/debug/ and src/release/. 
            -->

            <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="" />
            <activity
                android:name=".MapsActivity"
                android:label="Map">
                <activity
                    android:name=".SecondActivity"/>

                </activity>

                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>


        </application>

        </manifest>

请帮忙

这是CrashLog

FATAL EXCEPTION: main
                                                                        android.content.ActivityNotFoundException: Unable to find explicit activity   class {com.example.user.naviapp/com.example.user.naviapp.SecondActivity}; have you declared this activity in your AndroidManifest.xml?
                                                                          at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1512)
                                                                          at android.app.Instrumentation.execStartActivity(Instrumentation.java:1388)
                                                                          at android.app.Activity.startActivityForResult(Activity.java:3190)
                                                                          at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:871)
                                                                          at android.app.Activity.startActivity(Activity.java:3297)
                                                                          at com.example.user.naviapp.MapsActivity$1.onClick(MapsActivity.java:49)
                                                                          at android.view.View.performClick(View.java:3558)
                                                                          at android.view.View$PerformClick.run(View.java:14157)
                                                                          at android.os.Handler.handleCallback(Handler.java:605)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                          at android.os.Looper.loop(Looper.java:137)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:4514)
                                                                          at java.lang.reflect.Method.invokeNative(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:511)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
                                                                          at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

您以错误的方式在清单中宣布您的活动,请执行以下操作:

<activity
                android:name=".MapsActivity"
                android:label="Map">

                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
</activity>
<activity android:name=".SecondActivity"/>