我已经在Android Studio中设置了一个标准的Maps Activity。然后我需要添加一个'wrapper'类,以便实现一个与MapsActivity的OnMapReadyCallback不同的类。此wrapper类没有可视组件,不应创建视图,然后只需调用MapsActivity来创建Map视图。
所有内容都会编译,但运行时不显示任何视图。
如果我在创建Map后将退出循环器从dispose移动到MReady,我只会在日志中得到以下行。
E/连接服务:尝试为NetworkRequest [ id=10,legacyType= -1,[功能:互联网不受限制且值得信任] ]
但仍然没有视图出现。下面的代码更新为最新。也没有我的Log.d/i消息出现在日志中,虽然它被设置为详细。
代码-Android清单. xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.booxotel.smallgminterface">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name = ".SmallMap">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key" />
<activity
android:name="com.booxotel.smallgminterface.MapsActivity"
android:label="@string/title_activity_maps">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
SmallMap.java (part示例
public class SmallMap extends Application implements FREExtension {
public static final String TAG = "MapInterface";
private static GoogleMap gMap;
private static LatLng cmap;
private static MarksData md;
private Handler mHandler;
public static FREContext extensionContext;
public static Context appContext;
public static SettingsContentObserver mSettingsWatcher;
public ShowMap shm;
@Override
public void onCreate() {
super.onCreate();
appContext = getApplicationContext();
}
public class ShowMap extends Thread {
ShowMap() {
prepareThread(false);
}
public void prepareThread(Boolean quit){
Log.i(TAG, "prepare thread");
if (gMap == null && !quit) {
Looper.prepare();
mHandler = new Handler();
Intent intent = new Intent(SmallMap.appContext, MapsActivity.class);
appContext.startActivity(intent);
}else if(quit) {
Looper.myLooper().quit();
}
Looper.loop();
}
}
public class MReady implements MapReadyEvent {
@Override
public void MapReady() {
gMap = MapsActivity.getMap();
shm.prepareThread(true);
Log.i(TAG, "map ready");
extensionContext.dispatchStatusEventAsync("mapReady", "true");
}
}
@Override
public FREContext createContext(String contextType) {
shm = new ShowMap();
shm.prepareThread(false);
return new MapInterfaceContext();
}
@Override
public void dispose() {
Log.d(TAG, "Extension disposed.");
Context context = appContext.getApplicationContext();
context.getContentResolver().unregisterContentObserver(mSettingsWatcher);
appContext = null;
extensionContext = null;
mSettingsWatcher = null;
}
@Override
public void initialize() {
Log.d(TAG, "Extension initialized.");
}
}
MapsActivity.java
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
public static GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
MapReadyEvent mre = new MapReadyEvent() {
@Override
public void MapReady() {}
};
mre.MapReady();
}
public static GoogleMap getMap() {
return mMap;
}
}
MapReadyEvent.java
public interface MapReadyEvent {
void MapReady();
}
activity_maps.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="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.mysite.testmap.MapsActivity" />
</RelativeLayout>
1条答案
按热度按时间ny6fqffe1#
startActivity
不是从主线程调用的。请使用以下命令: