MrHarrison
MrHarrison

Reputation: 15

Incompatible Types while adding a button

I get the Error

'Incompatible types Required: android.widget.Button found: android.view.View'

Now there's an error with the class...

They said I shall rename the file, but I have a MapsActivity in my code too

This is my Code:

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

    @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(map);
        mapFragment.getMapAsync(this);

        final Toolbar customToolbar = (Toolbar) findViewById(R.id.toolbar);
        customToolbar.setTitle("Hello");
        getSupportActionBar().setTitle("Titeltest");

        getSupportActionBar().setDisplayShowTitleEnabled(false);
        TextView mTitle = (TextView) customToolbar.findViewById(R.id.toolbar_title);


    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

    }

}
public class ButtonClass extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_maps);

        final Button button = (Button) findViewById(R.id.addbutton);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //Insert Action if clicking on button here
            }
        });
    }
}

Upvotes: 0

Views: 1311

Answers (1)

Homayoon Ahmadi
Homayoon Ahmadi

Reputation: 2833

You have to cast your view to button:

final Button button = (Button) findViewById(R.id.addbutton);

edit:

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback {

    private GoogleMap mMap;

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

        final Button button = (Button) findViewById(R.id.addbutton);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //Insert Action if clicking on button here
            }
        });
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(map);
        mapFragment.getMapAsync(this);

        final Toolbar customToolbar = (Toolbar) findViewById(R.id.toolbar);
        customToolbar.setTitle("Hello");
        getSupportActionBar().setTitle("Titeltest");

        getSupportActionBar().setDisplayShowTitleEnabled(false);
        TextView mTitle = (TextView) customToolbar.findViewById(R.id.toolbar_title);


    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

    }

}

Upvotes: 2

Related Questions