I want to move a Google Map around on a view. To try to find out how to do it, I have built a simple test that attempts to move the map every 5 seconds from a timer as a demonstration (on a tablet). (I'm a beginner at Java by the way.)
The test project compiles but crashes when run and appears to crash because of the null in the line
View view = inflater.inflate(R.layout.activity_maps, null);
But if the only view is from setContentView(R.layout.activity_maps); at onCreate, what should null be changed to?
MapsActivity.java
activity_maps.xml
The map appears in the position and size set by activity_maps if I remove the timer and setLayout().
The error is error 2 no such file or directory and occurs at line 4008 “in Group main” whatever that means.
Can anyone point me in the right direction to solve the problem?
Doug
The test project compiles but crashes when run and appears to crash because of the null in the line
View view = inflater.inflate(R.layout.activity_maps, null);
But if the only view is from setContentView(R.layout.activity_maps); at onCreate, what should null be changed to?
MapsActivity.java
Code:
import android.content.Context;
import android.os.CountDownTimer;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
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.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
public GoogleMap mMap;
private int counter = 2;
[USER=1021285]@override[/USER]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
new CountDownTimer(60000, 5000) {
public void onTick(long millisUntilFinished) {
int xpos = 0;
int ypos = 0;
int width = 0;
int height = 0;
switch(counter){
case 1 :
xpos = 710;
ypos = 167;
width = 311;
height = 290;
break;
case 2 :
xpos = 215;
ypos = 150;
width = 935;
height = 495;
break;
case 3 :
xpos = 215;
ypos = 282;
width = 300;
height = 242;
break;
}
if(counter == 3)
counter = 0;
counter ++;
setLayout(xpos, ypos, width, height);
}
public void onFinish() {}
}.start();
}
private void setLayout(int xpos, int ypos, int width, int height) {
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.activity_maps, null);
LinearLayout layout = (LinearLayout) findViewById(view.getId());
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)layout.getLayoutParams();
params.setMargins(xpos, ypos, 0, 0);
params.width = width;
params.height = height;
layout.setLayoutParams(params);
setContentView(view);
}
[USER=1021285]@override[/USER]
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));
}
}
activity_maps.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:eek:rientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="311dp"
android:layout_height="290dp"
android:layout_marginLeft="710dp"
android:layout_marginTop="167dp"
tools:context="com.booxotel.testmap.MapsActivity" />
</LinearLayout>
The map appears in the position and size set by activity_maps if I remove the timer and setLayout().
The error is error 2 no such file or directory and occurs at line 4008 “in Group main” whatever that means.
Can anyone point me in the right direction to solve the problem?
Doug
Last edited by a moderator: