Since Google warned us to provide support to 64-bit architecture, I am migrating my existing Unity Project from version Unity 5.6.6f to Unity 2018.4.1f
Upon running my project app crashes with log,
2019-06-02 20:08:27.869 14987-14987/com.example.myapplication:unityplayer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication:unityplayer, PID: 14987
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.UnityActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2747)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2808)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1541)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6375)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x0
at android.content.res.Resources.getText(Resources.java:347)
at android.content.res.MiuiResources.getText(MiuiResources.java:97)
at android.content.res.Resources.getString(Resources.java:393)
at com.unity3d.player.UnityPlayer.a(Unknown Source)
at com.unity3d.player.UnityPlayer.<init>(Unknown Source)
at com.example.myapplication.UnityActivity.onCreate(UnityActivity.java:52)
at android.app.Activity.performCreate(Activity.java:6845)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
STEPS I HAVE DONE FROM MY END
I have created a new project in Unity 2018.4.1f and changed the scripting back-end configuration to IL2CPP to support 64-bit architecture.
Please see my configuration below.
I have changed the package name in Unity to that my Android app package name and exported the build.
After exporting, I have pasted the Library folder, Asset folder and JniLibs folder contents (which I got from exported Unity Android project) to my Android project respective folders.
My UnityActivity file in Android Studio Project,
package com.example.myapplication;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Window;
import android.widget.FrameLayout;
import com.unity3d.player.UnityPlayer;
public class UnityActivity extends Activity {
protected static UnityPlayer mUnityPlayer; // don't change the name of UnityPlayer.currentActivity variable; referenced from native code
#Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.RGBX_8888); // <--- UnityPlayer.currentActivity makes xperia play happy
mUnityPlayer = new UnityPlayer(this);
setContentView(mUnityPlayer);
FrameLayout cameraLayout = findViewById(R.id.unity_player_layout);
cameraLayout.addView(mUnityPlayer.getView());
mUnityPlayer.requestFocus();
}
#Override
protected void onNewIntent(Intent intent) {
setIntent(intent);
}
// Quit Unity
#Override
protected void onDestroy() {
if (mUnityPlayer != null) {
mUnityPlayer.quit();
}
super.onDestroy();
}
// Pause Unity
#Override
protected void onPause() {
super.onPause();
System.out.println("OnPause called");
mUnityPlayer.pause();
}
// Resume Unity
#Override
protected void onResume() {
super.onResume();
System.out.println("OnResume called");
mUnityPlayer.resume();
}
#Override
protected void onStop() {
super.onStop();
System.out.println("Onstop called");
}
// Low Memory Unity
#Override
public void onLowMemory() {
super.onLowMemory();
mUnityPlayer.lowMemory();
}
// Trim Memory Unity
#Override
public void onTrimMemory(int level) {
super.onTrimMemory(level);
if (level == TRIM_MEMORY_RUNNING_CRITICAL) {
mUnityPlayer.lowMemory();
}
}
// UnityPlayer.currentActivity ensures the layout will be correct.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mUnityPlayer.configurationChanged(newConfig);
}
// Notify Unity of the focus change.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
mUnityPlayer.windowFocusChanged(hasFocus);
}
// For some reason the multiple keyevent type is not supported by the ndk.
// Force event injection by overriding dispatchKeyEvent().
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_MULTIPLE)
return mUnityPlayer.injectEvent(event);
return super.dispatchKeyEvent(event);
}
// Pass any events not handled by (unfocused) views straight to UnityPlayer
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
return mUnityPlayer.injectEvent(event);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
return mUnityPlayer.injectEvent(event);
}
/*API12*/
public boolean onGenericMotionEvent(MotionEvent event) {
return mUnityPlayer.injectEvent(event);
}
#Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
}
#Override
public void onBackPressed() {
super.onBackPressed();
UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
#Override
public void run() {
mUnityPlayer.quit();
}
});
}
}
Corresponding XML file,
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/unity_player_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
My AndroidManifest file looks like
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />
<uses-feature
android:name="android.hardware.camera.front"
android:required="false" />
<uses-feature
android:name="android.hardware.camera.front.autofocus"
android:required="false" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="false" />
<uses-feature
android:name="android.hardware.touchscreen"
android:required="false" />
<uses-feature
android:name="android.hardware.touchscreen.multitouch"
android:required="false" />
<uses-feature
android:name="android.hardware.touchscreen.multitouch.distinct"
android:required="false" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<!-- Allows access to the flashlight -->
<permission
android:name="android.permission.FLASHLIGHT"
android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
android:protectionLevel="normal"
android:required="false" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:usesCleartextTraffic="true">
<uses-library
android:name="org.apache.http.legacy"
android:required="false" />
<activity
android:name=".UnityActivity"
android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection"
android:label="#string/app_name"
android:launchMode="singleTask"
android:process=":unityplayer"
android:screenOrientation="landscape">
<meta-data
android:name="unityplayer.UnityActivity"
android:value="true" />
<meta-data
android:name="unityplayer.ForwardNativeEventsToDalvik"
android:value="true" />
</activity>
<!--
To support devices using the TI S3D library for stereo mode we must
add the following library.
Devices that require this are: ODG X6
-->
<uses-library
android:name="com.ti.s3d"
android:required="false" /> <!-- To support the ODG R7 in stereo mode we must add the following library. -->
<uses-library
android:name="com.osterhoutgroup.api.ext"
android:required="false" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Since, I am a native developer and knows little about Unity, any help to sort out this issue will be really helpful!
I have to add the below line to strings.xml file to get rid of the error!
<string name="game_view_content_description">Game view</string>
Related
For some reason i can't see any map using the google play services API.
activity_map:
package com.m.get.dl;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
public class Map extends FragmentActivity implements OnMapReadyCallback
{
MapFragment mapFragment;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.fragment_id);
mapFragment.getMapAsync(Map.this);
}
#Override
public void onMapReady(GoogleMap googleMap)
{
googleMap.setMyLocationEnabled(true);
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); // Setting map type
}
#Override
protected void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
}
#Override
protected void onPause()
{
// TODO Auto-generated method stub
super.onPause();
}
#Override
protected void onResume()
{
// TODO Auto-generated method stub
super.onResume();
}
#Override
protected void onSaveInstanceState(Bundle outState)
{
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
}
#Override
protected void onStop()
{
// TODO Auto-generated method stub
super.onStop();
}
}
I think the problem lies in the XML file - activity_map.xml:
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:map="http://schemas.android.com/apk/res-auto"
class="com.google.android.gms.maps.MapFragment"
android:name="com.google.android.gms.maps.MapFragment"
android:id="#+id/fragment_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.m.get.dl"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="12"
android:targetSdkVersion="23" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="My key" />
<activity
android:name=".Map"
android:label="#string/title_activity_map" >
</activity>
</application>
</manifest>
So in the activity i get this screen instead of a map.
So is my fragment tag is wrong?
Check the server key you are using. Make sure you have used accurate SHA-1 and package name. Rest of the code looks good!
Also, check the log if you are getting any error like:
Authentication failed on the server...
This says your key is not correct.
Updated:
You will be getting this log after the 'Authentication failed...'
Ensure that the following Android Key exists: ...
You can get correct SHA-1 key and package name below this 'Ensure that...' error message.
Both are separated by ';'
And now create new API key and add this new key in Manifest file tag.
**Manifest:**
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="#string/google_maps_key" />
This will solve your problem!
I have just started learning about Unity so this may seem like a stupid question.
I have made a small minigame that renders a character when the device camera sees a specific image. My problem is that I want to show some kind of menu before the unity activity is triggered.
For example in my code, I changed my launcher activity to be my main activity and when the start button in pressed then I would like that the unity activity would be triggered (and my device camera will open). Unfortunately it's not working (my app crashes when I press the button) and my logcat doesn't show anything.
This is my manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="imp.diana.samurai"
android:installLocation="preferExternal"
android:theme="#android:style/Theme.NoTitleBar"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="23" />
<uses-feature android:name="android.hardware.camera" />
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:xlargeScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:glEsVersion="0x00020000" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />
<uses-feature
android:name="android.hardware.camera.front"
android:required="false" />
<uses-feature
android:name="android.hardware.touchscreen"
android:required="false" />
<uses-feature
android:name="android.hardware.touchscreen.multitouch"
android:required="false" />
<uses-feature
android:name="android.hardware.touchscreen.multitouch.distinct"
android:required="false" />
<android:uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:debuggable="false"
android:icon="#drawable/app_icon"
android:isGame="true"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
<activity
android:name=".UnityPlayerNativeActivity"
android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="fullSensor" >
<meta-data
android:name="unityplayer.UnityActivity"
android:value="true" />
<meta-data
android:name="unityplayer.ForwardNativeEventsToDalvik"
android:value="false" />
</activity>
<activity
android:name="com.unity3d.player.VideoPlayer"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen"
android:label="#string/app_name"
android:screenOrientation="portrait" >
</activity>
<!--
To support devices using the TI S3D library for stereo mode we must
add the following library.
Devices that require this are: ODG X6
-->
<uses-library
android:name="com.ti.s3d"
android:required="false" />
<!-- To support the ODG R7 in stereo mode we must add the following library. -->
<uses-library
android:name="com.osterhoutgroup.api.ext"
android:required="false" />
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest> <!-- android:installLocation="preferExternal" -->
Main Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button) findViewById(R.id.button);
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, UnityPlayer.class));
}
});
}
UnityPlayerActivity
public class UnityPlayerActivity extends Activity
{
protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code
// Setup activity layout
#Override protected void onCreate (Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia play happy
mUnityPlayer = new UnityPlayer(this);
if (mUnityPlayer.getSettings().getBoolean("hide_status_bar", true))
{
getWindow ().setFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
setContentView(mUnityPlayer);
mUnityPlayer.requestFocus();
}
// Quit Unity
#Override protected void onDestroy ()
{
mUnityPlayer.quit();
super.onDestroy();
}
// Pause Unity
#Override protected void onPause()
{
super.onPause();
mUnityPlayer.pause();
}
// Resume Unity
#Override protected void onResume()
{
super.onResume();
mUnityPlayer.resume();
}
// This ensures the layout will be correct.
#Override public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
mUnityPlayer.configurationChanged(newConfig);
}
// Notify Unity of the focus change.
#Override public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
mUnityPlayer.windowFocusChanged(hasFocus);
}
// For some reason the multiple keyevent type is not supported by the ndk.
// Force event injection by overriding dispatchKeyEvent().
#Override public boolean dispatchKeyEvent(KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_MULTIPLE)
return mUnityPlayer.injectEvent(event);
return super.dispatchKeyEvent(event);
}
// Pass any events not handled by (unfocused) views straight to UnityPlayer
#Override public boolean onKeyUp(int keyCode, KeyEvent event) { return mUnityPlayer.injectEvent(event); }
#Override public boolean onKeyDown(int keyCode, KeyEvent event) { return mUnityPlayer.injectEvent(event); }
#Override public boolean onTouchEvent(MotionEvent event) { return mUnityPlayer.injectEvent(event); }
/*API12*/ public boolean onGenericMotionEvent(MotionEvent event) { return mUnityPlayer.injectEvent(event); }
}
Any idea? Any help would be greatly appreciated!
You probably haven't listed UnityPlayerActivity in the AndroidManifest.xml file using the tags <activity></activity> e.g.
<activity
android:name=".UnityPlayerActivity"
android:label="#string/app_name"
...
>
</activity>
APP is in pause state. I am changing the language(localization) of the phone afterwards when I select the app from recent history(pause state to resume state). The following lifecycle methods are calling in following order
onDestroy
onCreate
onStart
onResume
onPause
onStop
onDestroy
but APP is not opening(view is not appearing).
Here I have given the source code of activity and android manifest file.
Any help is appreciated?
public class CordovaExample extends DroidGap {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.loadUrl("file:///android_asset/www/index.html");
Log.i("onCreate Called","onCreate Called");
}
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.i("OnStart Called","OnStart Called");
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.i("onResume Called","onResume Called");
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.i("onPause Called","onPause Called "+Locale.getDefault().getLanguage());
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.i("onStop Called","onStop Called");
}
#Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i("onDestroy Called","onDestroy Called");
}
}
manifestfile:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.apache.cordova.rest"
android:hardwareAccelerated="true"
android:versionCode="1"
android:versionName="1.0"
android:windowSoftInputMode="adjustPan" >
<supports-screens
android:anyDensity="true"
android:largeScreens="true"
android:normalScreens="true"
android:resizeable="true"
android:smallScreens="true"
android:xlargeScreens="true" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.RECORD_VIDEO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<application
android:hardwareAccelerated="true"
android:icon="#drawable/icon"
android:label="#string/app_name" >
<activity
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
android:name="org.apache.cordova.rest.CordovaRestClient"
android:label="#string/app_name"
android:theme="#android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
</manifest>
Open the following Java File in cordova library project
framework/src/org/apache/cordova/CordovaWebView.java
Replace the following line of code
if (url.startsWith("file://") || url.startsWith("javascript:") || Config.isUrlWhiteListed(url))
to
if (url.startsWith("file://") || url.startsWith("javascript:") || Config.isUrlWhiteListed(url)&& !url.equals("about:blank"))
It is working fine now
when I run in the Galaxy S3, appears the name Google and the +/- of zoom, but the map never appear, please someone help me!
I've done all the steps three times to generate API Key.
I don't know if I have to do something in Activity, but my activity extends FragmentActivity, but always I try get the map for this, always return null...
GoogleMap mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
My manifest:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<permission
android:name="com.example.portalrugby.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.portalrugby.permission.MAPS_RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-feature
android:name="android.hardware.location"
android:required="true" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="false"
android:icon="#drawable/icon340x340"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="xxxxxxxxxxxx" />
My activity_main.xml
<RelativeLayout
android:id="#+id/relativeMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:visibility="invisible" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
My logcat:
10-31 09:22:47.520: D/REQUEST(23871): Error processing: com.google.maps.api.android.lib6.b.d#4263b810 not retrying
10-31 09:22:47.520: D/REQUEST(23871): Error processing: com.google.maps.api.android.lib6.gmm6.d.h#426335b8 not retrying
10-31 09:22:47.520: I/Google Maps Android API(23871): Failed to contact Google servers. Another attempt will be made when connectivity is established.
10-31 09:22:51.265: D/REQUEST(23871): Error processing: com.google.maps.api.android.lib6.b.d#4365a3c0 not retrying
10-31 09:22:51.265: D/REQUEST(23871): Retrying: com.google.maps.api.android.lib6.b.j#4263e6f8
10-31 09:22:51.265: D/REQUEST(23871): Retrying: com.google.maps.api.android.lib6.c.au#426bbae8
10-31 09:22:51.475: D/REQUEST(23871): Connection opened to:https://clients4.google.com/glm/mmap/api
10-31 09:22:51.475: D/REQUEST(23871): Open Connection
10-31 09:22:51.765: D/REQUEST(23871): DRD(9): 62|15|147
10-31 09:22:51.765: D/REQUEST(23871): Close
Java Page code
package com.dilip.googlemapsv2;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
// Google Map
private GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* function to load map. If map is not created it will create it for you
* */
private void initilizeMap() {
if (googleMap == null) {
//googleMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
googleMap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),"Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
}
}
}
#Override
public void onResume() {
super.onResume();
GooglePlayServicesUtil.isGooglePlayServicesAvailable(this.getApplicationContext());
initilizeMap();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
XML Page Code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
Menefist Page Code
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dilip.googlemapsv2"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.dilip.googlemapsv2.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="20" />
<uses-permission android:name="com.dilip.googlemapsv2.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-feature android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Goolge API Key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="xxxxxxxxxxxxxxxxxxxxxxxxxxx" /> /* Added Bi Dilip */
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
i have created an android app on google maps through eclipse,there are no errors but when i install it on emulator or any android device and run it,error message is displayed that "unfortunately app has stopped working".
please guide me for the troubleshooting.
this is the java main activity file :-
package com.example.droidloc;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import android.view.Menu;
import android.view.MotionEvent;
public class MainActivity extends MapActivity {
MapView map;
long start;
long stop;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map=(MapView)findViewById(R.id.mainmap);
map.setBuiltInZoomControls(true);
touchy t =new touchy();
List<Overlay> overlayList = map.getOverlays();
overlayList.add(t);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
class touchy extends Overlay
{
#SuppressWarnings("deprecation")
public boolean onTouchEvent(MotionEvent e,MapView m)
{
if(e.getAction()==MotionEvent.ACTION_DOWN)
{
start=e.getEventTime();
}
if(e.getAction()==MotionEvent.ACTION_UP)
{
stop=e.getEventTime();
}
if(stop-start>1500)
{
AlertDialog alert=new
AlertDialog.Builder(MainActivity.this).create();
alert.setTitle("Pick an Option");
alert.setMessage("Pick an Option dude");
alert.setButton2("Pin a Point", new
DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface arg0, int arg1)
{
// TODO Auto-generated method stub
}
});
alert.show();
return true;
}
return false ;
}
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
---------------------------------------------------------------------------------------
This is the xml file :-
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainmap"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"
android:enabled="true"
android:clickable="true"
/>
---------------------------------------------------------------------------------------
this is the manifest file :-
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.droidloc"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name="com.example.droidloc.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAlHKiaTa8KrABuGHIvcuZ4IiBFifqOu1s"/>
</application>
</manifest>
I think Your Fragment can not cast to MapView. so it could be your classcastexception . if not than you shoud post your logcat output.
try by use this in your Layout may be solve problem:
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainmap"
android:layout_below="#+id/innerrelativelay" />
or
<fragment
android:id="#+id/mainmap"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
There are a bunch of things missing. Heres a list of what I saw, and some are from comments:
You are extending mapActivity which is based on versio 1 of google maps android. But in xml you are using mapfragment v2 of google maps android.
You should extend FragmentActivity or any other activity like ActionBarActivity that extends from FragmentActivity.
You are missing metadata tag in manifest file.
Min sdk version is 8 so you need to use SupportMapFragment in xml you are using mapfragment which is available above api 11.