我在这个视图中有一个按钮,它也有地图类,但不知怎的,我不能设置一个监听这个按钮:
fileBtn.setOnClickListener(this);
我不确定这是因为这是地图类/片段或任何其他原因。
这里的全班:
/**
* This shows how to listen to some {@link GoogleMap} events.
*/
public class MapsActivity extends AppCompatActivity
implements OnMapClickListener, OnMapLongClickListener,
OnMapReadyCallback, OnClickListener {
private TextView mTapTextView;
private Button fileBtn;
private static final int REQUEST_PICK_FILE = 1;
private File selectedFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
mTapTextView = (TextView) findViewById(R.id.tap_text);
mTapTextView.setText("Loaded");
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(map);
mapFragment.getMapAsync(this);
}
Button fileBtn = (Button) findViewById(R.id.loadfile);
fileBtn.setOnClickListener(this);
public void onClick(View v) {
switch(v.getId()) {
case R.id.loadfile:
Intent intent = new Intent(this, FilePicker.class);
startActivityForResult(intent, REQUEST_PICK_FILE);
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK) {
switch(requestCode) {
case REQUEST_PICK_FILE:
if(data.hasExtra(FilePicker.EXTRA_FILE_PATH)) {
selectedFile = new File
(data.getStringExtra(FilePicker.EXTRA_FILE_PATH));
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, "Test single attachment");
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"amirfarazmand@gmail.com"});
intent.putExtra(Intent.EXTRA_TEXT, "Mail with an attachment");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(data.getStringExtra(FilePicker.EXTRA_FILE_PATH))));
intent.setType("application/pdf");
startActivity(Intent.createChooser(intent, "Send mail"));
}
break;
}
}
}
private GoogleMap googleMap;
@Override
public void onMapReady(GoogleMap map) {
map.setOnMapClickListener(this);
map.setOnMapLongClickListener(this);
googleMap = map;
enableMyLocation();
}
/* private static final LatLng MELBOURNE = new LatLng(-37.81319, 144.96298);
private static final LatLng ADELAIDE = new LatLng(-34.92873, 138.59995);
private static final LatLng PERTH = new LatLng(-31.95285, 115.85734); */
public int i=0;
public Polygon polygon;
public Polyline polyline;
List<LatLng> coordinates=new ArrayList<LatLng>();
public void onMapClick(LatLng point) {
mTapTextView.setText("tapped, point=" + point);
if (i==0){
i=1;
coordinates.add(point);
googleMap.addMarker(new MarkerOptions()
.position(point)
.title(String.valueOf(point.latitude))
.snippet(String.valueOf(point.latitude))
.rotation((float) -15.0)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE))
);
} else if (i==1) {
i=i+1;
coordinates.add(point);
polyline = googleMap.addPolyline((new PolylineOptions())
.add(coordinates.get(0), coordinates.get(1)));
}else if (i>1){
coordinates.add(point);
polyline.remove();
if (i>2){polygon.remove();};
polygon = googleMap.addPolygon(new PolygonOptions()
.addAll(coordinates)
.strokeColor(Color.BLACK)
.strokeWidth(10));
//polygon = googleMap.addPolygon((new PolygonOptions())
// .add(coordinates.get(0), coordinates.get(1),coordinates.get(2)));
i=i+1;
}/*else{
List<LatLng> polygonList = polygon.getPoints();
Toast.makeText(getBaseContext(), polygonList.toString(), Toast.LENGTH_LONG).show();
polygonList.add(point);
polygon.remove();
polygon = googleMap.addPolygon((new PolygonOptions()));
polygon.setPoints(polygonList);
i=i+1;
}*/
}
public boolean mapView = true;
@Override
public void onMapLongClick(LatLng point) {
if(mapView) {
mTapTextView.setText("long pressed, point=" + point);
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mapView=!mapView;
}else{
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mapView=!mapView;
}
}
//Load coorodinates from file
private static final int LOCATION_PERMISSION_REQUEST_CODE = 1;
private void enableMyLocation() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Permission to access the location is missing.
PermissionUtils.requestPermission(this, LOCATION_PERMISSION_REQUEST_CODE,
Manifest.permission.ACCESS_FINE_LOCATION, true);
} else if (googleMap != null) {
// Access to the location has been granted to the app.
googleMap.setMyLocationEnabled(true);
googleMap.setTrafficEnabled(true);
}
}
/**
* Displays a dialog with error message explaining that the location permission is missing.
*/
private void showMissingPermissionError() {
PermissionUtils.PermissionDeniedDialog
.newInstance(true).show(getSupportFragmentManager(), "dialog");
}
}
答案 0 :(得分:2)
我认为你需要在onCreate()方法中而不是在外面声明按钮并单击listner 它。喜欢:
oncreate()
{
b1=(Button)findviewbyId(R.id.abc);
b1.setOnclicklistener(this);
}
当您实现View.Onclicklistener时...您将获得覆盖方法onClick ....在基于id的内部,你实现了你的逻辑。
答案 1 :(得分:1)
您应该在onCreate
中添加侦听器。然后是override
的{{1}}方法。
onClick
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
mTapTextView = (TextView) findViewById(R.id.tap_text);
mTapTextView.setText("Loaded");
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(map);
mapFragment.getMapAsync(this);
Button fileBtn = (Button) findViewById(R.id.loadfile);
fileBtn.setOnClickListener(this);
}
方法。
Override
答案 2 :(得分:0)
为什么这些行
Button fileBtn = (Button) findViewById(R.id.loadfile);
fileBtn.setOnClickListener(this);
在onCreate()方法之外?
答案 3 :(得分:0)
原因是因为你的类实现了OnClickListener
而不是View.OnClickListener
答案 4 :(得分:0)
Button fileBtn = (Button) findViewById(R.id.loadfile);
fileBtn.setOnClickListener(this);
您的代码令人头晕,我理解您需要将此代码放在onCreate中。