2017-08-02 24 views
0

我得到的錯誤不兼容的類型,同時增加一個按鈕

「不兼容類型 要求:android.widget.Button 發現:android.view.View」

現在有與類的錯誤.. 。

他們說我要重命名文件,但我有一個MapsActivity在我的代碼太

這是我的代碼:

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

回答

2

你要投你的視圖按鈕:

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

編輯:

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

    } 

} 
+0

做到了。但現在一個錯誤「類ButtonClass是公共的,應在名爲ButtonClass.java文件中聲明」出現 – MrHarrison

+0

你應該你的類文件重命名爲ButtonClass.java – Nobody8

+0

@MrHarrison更改文件名的類名。 –