I've got an android project from another developer. He can launch this project on his machine successfully, but when I try to launch it on my machine, I've got this exception when application trying load map in a fragment. The logcat with exception:
09-29 07:37:50.298 2442-2442/nz.co.tish.android.coffeetime E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{nz.co.tish.android.coffeetime/nz.co.tish.android.coffeetime.fragment.MapFragment$InnerActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.startActivityNow(ActivityThread.java:2054)
at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
at nz.co.tish.android.coffeetime.fragment.MapFragment.onCreate(MapFragment.java:132)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1477)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:893)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
at android.view.View.measure(View.java:15848)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:651)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:302)
at android.view.View.measure(View.java:15848)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
at android.view.View.measure(View.java:15848)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
at android.app.Activity.setContentView(Activity.java:1895)
at nz.co.tish.android.coffeetime.fragment.MapFragmen
09-29 07:42:50.399 2442-2442/nz.co.tish.android.coffeetime I/Process﹕ Sending signal. PID: 2442 SIG: 9
Android manifest:
android:versionCode="7"
android:versionName="1.3" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.VIBRATE" />
<permission
android:name="nz.co.tish.android.coffeetime.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="nz.co.tish.android.coffeetime.permission.C2D_MESSAGE" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:name="nz.co.tish.android.coffeetime.CoffeetimeApplication"
android:allowBackup="true"
android:description="#string/app_desc"
android:icon="#drawable/app_logo57"
android:label="Coffeetime TISH Limited"
android:screenOrientation="portrait"
android:theme="#style/AppTheme" >
<uses-library android:name="com.google.android.maps" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyDMx6IbAJEWnHuKv_yv4Pkwgx_U1mF2Qkc" />
<!--Debug: AIzaSyDMx6IbAJEWnHuKv_yv4Pkwgx_U1mF2Qkc-->
<!--Live: AIzaSyCnNpNptAzaBdtF9GOAVw-POo1gt2Tsw-Q-->
<meta-data
android:name="com.google.android.gms.versionCode"
android:value="#integer/google_play_services_version" />
<activity
android:name="nz.co.tish.android.coffeetime.MainActivity"
android:label="#string/title_activity_main"
android:screenOrientation="portrait"
android:theme="#style/AppMainTheme" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.PlaceActivity"
android:label="#string/title_activity_place"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.ForgotPasswordActivity"
android:label="#string/title_activity_forgot_password"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.CVCInfoActivity"
android:label="#string/cvc_text_header"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.CategoryActivity"
android:label="#string/title_activity_category"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="nz.co.tish.android.coffeetime.MainActivity" />
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.ProductActivity"
android:label="#string/title_activity_product"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="nz.co.tish.android.coffeetime.CategoryActivity" />
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.CheckoutActivity"
android:label="#string/title_activity_checkout"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="nz.co.tish.android.coffeetime.PlaceActivity" />
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.TopupActivity"
android:label="#string/title_activity_topup"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.RegisterCardActivity"
android:label="#string/title_activity_register_card"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.StoreMapActivity"
android:label="#string/store_map_title_activity"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.AuthActivity"
android:label="#string/title_activity_auth"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.VersionInfoActivity"
android:label="#string/title_activity_version"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="nz.co.tish.android.coffeetime.AuthActivity" />
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.TermsActivity"
android:label="#string/title_activity_terms"
android:screenOrientation="portrait" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.VIEW" />
<data android:scheme="launch.TermsActivity" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="nz.co.tish.android.coffeetime.AuthActivity" />
</activity>
<activity
android:name="nz.co.tish.android.coffeetime.fragment.MapFragment$InnerActivity"
android:screenOrientation="portrait" />
<activity
android:name=".LogoutActivity"
android:label="#string/title_activity_logout"
android:screenOrientation="portrait" />
<activity
android:name=".LikeUsOnFacebookActivity"
android:label="Like Us On Facebook Activity"
android:screenOrientation="portrait" />
</application>
The code of fragment with map(MapFragment):
public class MapFragment extends Fragment implements LocationListener {
private static GoogleMap mapView;
private static final String MAP_ACTIVITY_ID = "map-activity";
private static final String ACTIVITY_MANAGER_BUNDLE_KEY = "activity-manager-bundle-key";
private LocalActivityManager localActivityManager;
private LocationManager locationManager = null;
private Window window;
private ProgressDialog dialog = null;
private String bestProvider = null;
private Criteria criteria = null;
private String keyword = "";
private Place[] places;
private HashMap<String, Place> currentPlaces;
private AsyncTask<Object, Object, CallResult<List<Place>>> location_task;
public static class InnerActivity extends
FragmentActivity {
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.fragment_map);
mapView = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.mapv2)).getMap();
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = window.getDecorView();
keyword = ((MainActivity) getActivity()).getSearchString();
locationManager = (LocationManager) view.getContext().getSystemService(
Activity.LOCATION_SERVICE);
if (locationManager == null) {
DialogUtil.showAlert(view.getContext(),
"Sorry, you do not seem to have GPS!");
return null;
}
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
criteria = new Criteria();
bestProvider = locationManager.getBestProvider(criteria, false);
return view;
}
#Override
public void onDestroyView() {
super.onDestroyView();
((ViewGroup) window.getDecorView().getParent()).removeAllViews();
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBundle(ACTIVITY_MANAGER_BUNDLE_KEY,
localActivityManager.saveInstanceState());
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = null;
if (savedInstanceState != null) {
bundle = savedInstanceState.getBundle(ACTIVITY_MANAGER_BUNDLE_KEY);
}
localActivityManager = new LocalActivityManager(getActivity(), true);
localActivityManager.dispatchCreate(bundle);
window = localActivityManager.startActivity(MAP_ACTIVITY_ID,
new Intent(getActivity(), InnerActivity.class));
}
#Override
public void onResume() {
super.onResume();
localActivityManager.dispatchResume();
places = ((MainActivity) getActivity()).getPlaces();
location_task = null;
if (places == null) {
AsyncTask<Object, Object, Location> task = new AsyncTask<Object, Object, Location>() {
#Override
protected Location doInBackground(Object... params) {
return locationManager.getLastKnownLocation(bestProvider);
}
#Override
protected void onPostExecute(Location result) {
onLocationChanged(result);
}
};
task.execute();
locationManager.requestLocationUpdates(bestProvider, 1000 * 60 * 5,
25, this);
} else {
setUpMapview(places);
}
}
private void setUpMapview(Place[] places) {
// List<Overlay> mapOverlays = mapView.getOverlays();
Drawable openPlace = this.getResources().getDrawable(R.drawable.opened);
Drawable closedPlace = this.getResources().getDrawable(
R.drawable.closed);
List<GeoPoint> items = new ArrayList<GeoPoint>();
currentPlaces = new HashMap<String, Place>();
mapView.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker coffe) {
if (currentPlaces != null) {
Intent intent = new Intent(getActivity(),
PlaceActivity.class);
intent.putExtra("place",
currentPlaces.get(coffe.getTitle()));
startActivity(intent);
}
}
});
for (Place place : places) {
GeoPoint point = new GeoPoint((int) (place.getLatitude()),
(int) (place.getLongitude()));
items.add(point);
Marker placeMarker = mapView
.addMarker(new MarkerOptions()
.position(
new LatLng(place.getLatitude(), place
.getLongitude()))
.title(place.getName())
.icon(BitmapDescriptorFactory.fromResource(place
.isOpen() ? R.drawable.opened
: R.drawable.closed)));
currentPlaces.put(place.getName(), place);
// itemizedOverlay.addOverlay(overlayitem);
// mapOverlays.add(itemizedOverlay);
}
MapUtils.animateToCenter(mapView, items, false);
}
#Override
public void onPause() {
super.onPause();
localActivityManager.dispatchPause(getActivity().isFinishing());
}
#Override
public void onStop() {
super.onStop();
localActivityManager.dispatchStop();
}
#Override
public void onDestroy() {
super.onDestroy();
localActivityManager.dispatchDestroy(getActivity().isFinishing());
}
#Override
public void onDetach() {
super.onDetach();
localActivityManager = null;
}
private class DialogOverlay extends SimpleItemizedOverlay {
public DialogOverlay(Drawable defaultMarker, MapView mapView) {
super(defaultMarker, mapView);
}
#Override
protected boolean onBalloonTap(int index, OverlayItem item) {
/*
* Intent intent = new Intent(mapView.getContext(),
* PlaceActivity.class); intent.putExtra("place",
* ((DialogOverlayItem)item).getCurrentPlace());
* startActivity(intent);
*/
return true;
}
}
private class DialogOverlayItem extends OverlayItem {
private Place currentPlace;
public DialogOverlayItem(GeoPoint point, String title, String snippet,
Place place) {
super(point, title, snippet);
setCurrentPlace(place);
}
private Place getCurrentPlace() {
return currentPlace;
}
private void setCurrentPlace(Place currentPlace) {
this.currentPlace = currentPlace;
}
}
}
fragment_map.xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapv2"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
MainActivity:
public class MainActivity extends FragmentActivity implements
ActionBar.TabListener {
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private SectionsPagerAdapter mSectionsPagerAdapter;
CustomViewPager mViewPager;
private Place[] places;
private String keyword = "";
private boolean checkedOut;
public Place[] getPlaces() {
return places;
}
public void setPlaces(Place[] places) {
this.places = places;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new MyFavoritesFragment();
case 1:
return new NearMeFragment();
case 2:
return new MapFragment();
case 3:
return new HistoryFragment(checkedOut);
case 4:
return new WalletFragment();
}
return null;
}
#Override
public int getCount() {
return 5;
}
public int getPageIcon(int position) {
switch (position) {
case 0:
return R.drawable.icon_fav;
case 1:
return R.drawable.icon_near;
case 2:
return R.drawable.icon_map;
case 3:
return R.drawable.icon_hist;
case 4:
return R.drawable.icon_wallet;
}
return 0;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.title_activity_my_favorites).toUpperCase();
case 1:
return getString(R.string.title_activity_near_me).toUpperCase();
case 2:
return getString(R.string.title_activity_map).toUpperCase();
case 3:
return getString(R.string.title_activity_history).toUpperCase();
case 4:
return getString(R.string.title_activity_wallet).toUpperCase();
}
return null;
}
}
}
This application also has another Activity with map , and the app is crashing on it too. The logcat:
09-29 08:33:50.559 2604-2604/nz.co.tish.android.coffeetime E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{nz.co.tish.android.coffeetime/nz.co.tish.android.coffeetime.StoreMapActivity}: android.view.InflateException: Binary XML file line #3: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
Caused by: java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value. Expected 4242000 but found 0. You must have the following declaration within the <application> element: <meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
at com.google.android.gms.common.GooglePlayServicesUtil.n(Unknown Source)
at com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(Unknown Source)
at com.google.android.gms.maps.internal.q.v(Unknown Source)
at com.google.android.gms.maps.internal.q.u(Unknown Source)
at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment$b.ex(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment$b.a(Unknown Source)
at com.google.android.gms.dynamic.a.a(Unknown Source)
at com.google.android.gms.dynamic.a.onInflate(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:290)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
StoreMapActivity:
public class StoreMapActivity extends FragmentActivity {
private Place place = null;
private GoogleMap mapView;
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return (true);
}
return (super.onOptionsItemSelected(item));
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.store_map_activity);
mapView = (GoogleMap) ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.store_mapv2)).getMap();
Serializable placeSer = getIntent().getSerializableExtra("place");
if (placeSer == null
|| !Place.class.isAssignableFrom(placeSer.getClass())) {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
finish();
return;
}
place = (Place) placeSer;
setUpMapview();
/*List<Overlay> mapOverlays = mapView.getOverlays();
Drawable openPlace = this.getResources().getDrawable(R.drawable.opened);
Drawable closedPlace = this.getResources().getDrawable(
R.drawable.closed);
List<GeoPoint> items = new ArrayList<GeoPoint>();
SimpleItemizedOverlay itemizedOverlay = new SimpleItemizedOverlay(
place.isOpen() ? openPlace : closedPlace, mapView);
GeoPoint point = new GeoPoint(
(int) (place.getLatitude() * Const.GEOPOINT_CONST),
(int) (place.getLongitude() * Const.GEOPOINT_CONST));
items.add(point);
OverlayItem overlayitem = new OverlayItem(point, place.getName(), "");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
// MapUtils.animateToCenter(mapView, items, true);*/
}
private void setUpMapview() {
Drawable openPlace = this.getResources().getDrawable(R.drawable.opened);
Drawable closedPlace = this.getResources().getDrawable(
R.drawable.closed);
List<GeoPoint> items = new ArrayList<GeoPoint>();
mapView.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker coffe) {
if (place != null) {
Intent intent = new Intent(StoreMapActivity.this,
PlaceActivity.class);
intent.putExtra("place", place);
startActivity(intent);
}
}
});
GeoPoint point = new GeoPoint((int) (place.getLatitude()),(int)(place.getLongitude()));
items.add(point);
Marker placeMarker = mapView.addMarker(new MarkerOptions().position(
new LatLng(place.getLatitude(), place.getLongitude()))
.title(place.getName())
.icon(BitmapDescriptorFactory.fromResource(place
.isOpen() ? R.drawable.opened
: R.drawable.closed)));
MapUtils.animateToCenter(mapView, items, false);
}
}
store_map_activity.xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/store_mapv2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"/>
When I commented the code initializing the maps on these parts of application, it will start working normally.
Please verify that you have added the exceptions to your proguard configuration file. How and what to add is explained in Setting up Google Play Services
For the first, change your fragment from this:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapv2"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
to this:
<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
the main difference in this case is
class="com.google.android.gms.maps.SupportMapFragment"
to this:
android:name="com.google.android.gms.maps.SupportMapFragment"
For the second: add the following to the end of your manifest (but beforde the end-tag application!)
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
In your code you can ask for the service with the following:
// check status
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
try {
if (status != ConnectionResult.SUCCESS)
{
GooglePlayServicesUtil.getErrorDialog(status, this, RQS_GooglePlayServices).show();
}
} catch (Exception e) {
Log.e("Error: ", + e);
}
Related
I want to install new version of my app programmatically. with targetSdkVersion=23 app works fine and this is my code:
public class MainActivity extends AppCompatActivity {
private int PERMISSION_REQUEST_STORAGE = 0;
private DownloadController downloadController;
Button btn_download;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_download = findViewById(R.id.buttonDownload);
btn_download.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkStoragePermission();
}
});
}
public void checkStoragePermission() {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
String url= getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + "/example.apk";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(Uri.parse("file://" + url), "application/vnd.android.package-archive");
String selfPackageName = getApplicationContext().getPackageName();
ComponentName componentName = intent.resolveActivity(getApplicationContext().getPackageManager());
String otherPackageName = (componentName != null ? componentName.getPackageName() : "");
if (MainActivity.this == null || !selfPackageName.equals(otherPackageName)) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
startActivity(intent);
} else {
requestStoragePermission();
}
}
public void requestStoragePermission() {
ActivityCompat.requestPermissions(
MainActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_REQUEST_STORAGE);
}
}
but with targetSdkVersion=29 I heard that I have to use FileProvider. I used this instructure but unfortunately it dosent open my apk to install. this is my res=>xml=>file_provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<files-path
name="files"
path="." />
</paths>
my manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mysms">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<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">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".service.OnComplete" >
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.myapp"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_provider_paths" />
</provider>
</application>
</manifest>
my mainActivity
public class MainActivity extends AppCompatActivity {
private int PERMISSION_REQUEST_STORAGE = 0;
private DownloadController downloadController;
Button btn_download;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_download = findViewById(R.id.buttonDownload);
btn_download.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkStoragePermission();
}
});
}
public void checkStoragePermission() {
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
File file = new File(getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString() + "/example.apk");
Uri uri = FileProvider.getUriForFile(getApplicationContext(), "com.example.myapp", file);
Intent install = new Intent(Intent.ACTION_VIEW);
install.setData(Uri.fromFile(file));
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
install.setClassName("com.example.myapp", "com.example.myapp.MainActivity");
install.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
ClipData clipData = new ClipData(new ClipDescription("Meshes", new String[]{ClipDescription.MIMETYPE_TEXT_URILIST}), new ClipData.Item(uri));
install.setClipData(clipData);
startActivity(install);
} else {
requestStoragePermission();
}
}
public void requestStoragePermission() {
ActivityCompat.requestPermissions(
MainActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
PERMISSION_REQUEST_STORAGE);
}
}
I get this error
2019-12-17 00:46:38.035 32020-32020/com.example.myapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mysms, PID: 32020
android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/com.example.myapp/files/Download/example.apk exposed beyond app through Intent.getData()
at android.os.StrictMode.onFileUriExposed(StrictMode.java:1799)
at android.net.Uri.checkFileUriExposed(Uri.java:2346)
at android.content.Intent.prepareToLeaveProcess(Intent.java:8933)
at android.content.Intent.prepareToLeaveProcess(Intent.java:8894)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1517)
at android.app.Activity.startActivityForResult(Activity.java:4224)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
at android.app.Activity.startActivityForResult(Activity.java:4183)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754)
at android.app.Activity.startActivity(Activity.java:4507)
at android.app.Activity.startActivity(Activity.java:4475)
at com.example.mysms.MainActivity.checkStoragePermission(MainActivity.java:107)
at com.example.mysms.MainActivity$3.onClick(MainActivity.java:76)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
It seems the problem is that you set wrong uri to intent data:
install.setData(Uri.fromFile(file));
You need to set uri which was created with FileProvider:
Uri uri = FileProvider.getUriForFile(getApplicationContext(), "com.example.myapp", file);
install.setData(uri);
The same is done in the instruction you have found:
val contentUri = FileProvider.getUriForFile(
context,
BuildConfig.APPLICATION_ID + PROVIDER_PATH,
File(destination)
)
val install = Intent(Intent.ACTION_VIEW)
install.data = contentUri
I am using a simple code to open up a camera on opencv the app works fine on LG G Pro but crashes on samsung galaxy S4
Please Note that this activity is called via another activity through intent.
GameActivity.java
public class GameActivity extends AppCompatActivity implements CameraBridgeViewBase.CvCameraViewListener2 {
public static final String MY_TAG = "MY_CUSTOM_MESSAGE";
private JavaCameraView javaCameraView;
private BaseLoaderCallback baseLoaderCallback= new BaseLoaderCallback(this) {
#Override
public void onManagerConnected(int status) {
switch (status){
case LoaderCallbackInterface.SUCCESS:
{
Log.i(MY_TAG, "OPENCV Loaded");
javaCameraView.enableView();
break;
}
default:
super.onManagerConnected(status);
}
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Log.i(MY_TAG, "onCreate Act 2");
javaCameraView=(JavaCameraView)findViewById(R.id.MyJavaCam);
javaCameraView.setVisibility(SurfaceView.VISIBLE);
javaCameraView.setCvCameraViewListener(this);
}
#Override
protected void onResume() {
super.onResume();
Log.i(MY_TAG, "onResume Act 2");
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_11, this, baseLoaderCallback);
Log.i(MY_TAG, "onResume Act 2 done resuming");
}
#Override
protected void onPause() {
super.onPause();
Log.i(MY_TAG, "onPause Act 2");
}
#Override
public void onCameraViewStarted(int width, int height) {
}
#Override
public void onCameraViewStopped() {
}
#Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
return inputFrame.rgba();
}
#Override
protected void onDestroy() {
super.onDestroy();
if(javaCameraView != null)
{
javaCameraView.disableView();
}
}
}
content_game.xml
<org.opencv.android.JavaCameraView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"
android:id="#+id/MyJavaCam"
opencv:show_fps="true"
opencv:camera_id="any"
/>
AndroidManifest.xml
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".GameActivity"
android:label="#string/title_activity_game"
android:theme="#style/AppTheme.NoActionBar"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="com.roundcube.gamewithserver.GameActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Logcat
07-10 04:13:48.107 11233-11233/com.roundcube.gamewithserver E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.roundcube.gamewithserver, PID: 11233
java.lang.RuntimeException: Unable to resume activity {com.roundcube.gamewithserver/com.roundcube.gamewithserver.GameActivity}: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=org.opencv.engine.BIND }
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3403)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3434)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2772)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Caused by: java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=org.opencv.engine.BIND }
at android.app.ContextImpl.validateServiceIntent(ContextImpl.java:2052)
at android.app.ContextImpl.bindServiceCommon(ContextImpl.java:2160)
at android.app.ContextImpl.bindService(ContextImpl.java:2138)
at android.content.ContextWrapper.bindService(ContextWrapper.java:559)
at org.opencv.android.AsyncServiceHelper.initOpenCV(AsyncServiceHelper.java:24)
at org.opencv.android.OpenCVLoader.initAsync(OpenCVLoader.java:89)
at com.roundcube.gamewithserver.GameActivity.onResume(GameActivity.java:85)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1255)
at android.app.Activity.performResume(Activity.java:6412)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3392)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3434)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2772)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1449)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5951)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
On using Logs i figured out that problem in this function call
`OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_11, this, baseLoaderCallback);
Please help me find a solution to this thank you.
I'm trying to read some data from an online server and add them dynamically in a ListView. Unfortunately it crashes with an android.content.ActivityNotFoundExceptionbut my activity is declared in the manifest so I don't understand what the problem is.
This is my java file:
public class Marker extends Activity {
// Retrieve COMMENTS from database
String myJSON;
private static final String TAG_RESULTS="result";
private static final String TAG_ID = "id";
private static final String TAG_POI = "poi";
private static final String TAG_USERNAME ="username";
private static final String TAG_COMMENT = "comment";
JSONArray comments = null;
// ListView
private ListView lv;
private ArrayList<String> strArr;
private ArrayAdapter<String> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_marker);
// Retrieve COMMENTS from database
getData();
}
// Retrieve COMMENTS from database
protected void showList(){
try {
lv = (ListView) findViewById(R.id.list);
strArr = new ArrayList<String>();
JSONObject jsonObj = new JSONObject(myJSON);
comments = jsonObj.getJSONArray(TAG_RESULTS);
for(int i=0;i<comments.length();i++){
JSONObject c = comments.getJSONObject(i);
Integer id = c.getInt(TAG_ID);
String name = c.getString(TAG_POI);
String username = c.getString(TAG_USERNAME);
String comment = c.getString(TAG_COMMENT);
strArr.add("Row:" + comment);
}
adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, strArr);
lv.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
public void getData(){
class GetDataJSON extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://xxxx/comments_out.php");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
#Override
protected void onPostExecute(String result){
myJSON=result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_marker_page, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
XML file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.diana.track.MarkerPage">
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
My Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.diana.track" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<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" />
<activity
android:name=".myprofile"
android:label="#string/title_activity_myprofile" >
</activity>
<activity
android:name=".Track"
android:label="#string/title_activity_track" >
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="#string/google_maps_key" />
<activity
android:name=".map"
android:label="#string/title_activity_map" >
</activity>
<activity
android:name=".NotificationView"
android:label="#string/title_activity_notification_view" >
</activity>
<activity
android:name=".ProximityActivity"
android:label="#string/title_activity_proximity" >
</activity>
<activity
android:name=".AddPoi"
android:label="#string/title_activity_add_poi" >
</activity>
<activity
android:name=".ShortPath"
android:label="#string/title_activity_short_path" >
</activity>
<activity
android:name=".profile"
android:label="#string/title_activity_profile" >
</activity>
<activity
android:name=".Marker"
android:label="#string/title_activity_marker" >
</activity>
</application>
</manifest>
and my logcat
07-16 18:34:27.659 8628-9083/com.example.diana.track D/-heap﹕ GC_FOR_ALLOC freed 3574K, 38% free 7122K/11376K, paused 57ms, total 65ms
07-16 18:34:28.059 8628-8628/com.example.diana.track I/Choreographer﹕ Skipped 37 frames! The application may be doing too much work on its main thread.
07-16 18:34:28.440 8628-9075/com.example.diana.track D/-heap﹕ GC_FOR_ALLOC freed 1605K, 35% free 7482K/11376K, paused 34ms, total 34ms
07-16 18:34:29.050 8628-9083/com.example.diana.track D/-heap﹕ GC_FOR_ALLOC freed 501K, 33% free 7648K/11376K, paused 33ms, total 35ms
07-16 18:34:29.050 8628-9083/com.example.diana.track I/dalvikvm-heap﹕ Grow heap (frag case) to 11.080MB for 1048592-byte allocation
07-16 18:34:30.041 8628-8628/com.example.diana.track D/AndroidRuntime﹕ Shutting down VM
07-16 18:34:30.041 8628-8628/com.example.diana.track W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415478b0)
07-16 18:34:30.061 8628-8628/com.example.diana.track E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.diana.track/com.google.android.gms.maps.model.Marker}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1628)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
at android.app.Activity.startActivityForResult(Activity.java:3390)
at android.app.Activity.startActivityForResult(Activity.java:3351)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
at android.app.Activity.startActivity(Activity.java:3587)
at android.app.Activity.startActivity(Activity.java:3555)
at com.example.diana.track.map.onMarkerClick(map.java:765)
at com.google.android.gms.maps.GoogleMap$10.zza(Unknown Source)
at com.google.android.gms.maps.internal.zzn$zza.onTransact(Unknown Source)
at android.os.Binder.transact(Binder.java:347)
at com.google.android.gms.maps.internal.bd.a(SourceFile:84)
at com.google.maps.api.android.lib6.d.as.b(Unknown Source)
at com.google.maps.api.android.lib6.gmm6.c.e.a(Unknown Source)
at com.google.maps.api.android.lib6.gmm6.n.av.a(Unknown Source)
at com.google.maps.api.android.lib6.gmm6.n.be.a(Unknown Source)
at com.google.maps.api.android.lib6.gmm6.n.bd.a(Unknown Source)
at com.google.maps.api.android.lib6.gmm6.n.bt.d(Unknown Source)
at com.google.maps.api.android.lib6.gmm6.n.ak.onSingleTapConfirmed(Unknown Source)
at com.google.maps.api.android.lib6.gmm6.h.g.onSingleTapConfirmed(Unknown Source)
at com.google.maps.api.android.lib6.gmm6.h.i.handleMessage(Unknown Source)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5214)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
at dalvik.system.NativeStart.main(Native Method)
Any idea what might be the problem?
my app crashes without any error message at googleApiClient.connect(). It never gets to onConnectionFailed. Here's what I have:
at MainActivity
public class MainMenu extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
public static final int REQUEST_LEADERBOARD = 1;
public static int SCREEN_WIDTH;
public static int SCREEN_HEIGHT;
private GoogleApiClient googleApiClient;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
SCREEN_WIDTH = metrics.widthPixels;
SCREEN_HEIGHT = metrics.heightPixels;
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(Games.API).addScope(Games.SCOPE_GAMES)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
public void newGame(View view) {
Intent intent = new Intent(MainMenu.this, Game.class);
startActivity(intent);
finish();
}
public void highScore(View view) {
if (isSignedIn()) {
startActivityForResult(Games.Leaderboards.getLeaderboardIntent(googleApiClient,
String.valueOf(R.string.leaderboard_id)), REQUEST_LEADERBOARD);
}
else {
System.out.println("not sign in");
}
}
#Override
public void onConnected(Bundle bundle) {
}
#Override
public void onConnectionSuspended(int i) {
}
#Override
public void onConnectionFailed(ConnectionResult connectionResult) {
System.out.println("failed");
}
#Override
protected void onStart() {
super.onStart();
googleApiClient.connect(); //here it crashed
}
private boolean isSignedIn() {
return (googleApiClient != null && googleApiClient.isConnected());
}
}
What I've done:
I have generated signed APK, I have sha1 key. I published the app for alpha testing.
manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.package"
android:versionCode="2"
android:versionName="1.1">
<uses-sdk android:minSdkVersion="1"/>
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.NoActionBar"
android:isGame="true" >
<meta-data
android:name="com.google.android.gms.games.APP_ID"
android:value="#string/app_id" />
<meta-data
android:name="my.package.version"
android:value="com.google.android.gms.version" />
<activity android:name=".activities.MainMenu">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".activities.Game"/>
<activity android:name=".activities.GameOver"/>
<activity android:name=".activities.HighScore"/>
</application>
</manifest>
I'm testing it on device with Google Play Service installed. Could you help me?
You should add the internet permission to your manifest.
<uses-permission android:name="android.permission.INTERNET"/>
If you need more help, you should show the error stacktrace.
Hope it helps you!
The solution is simple
<meta-data
android:name="my.package.version"
android:value="com.google.android.gms.version" />
is nonsense.
it should be:
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
java.lang.ClassCastException: android.app.Application cannot be cast to com.example.project.DataDevice
My code:
public class Project extends Activity{
private boolean connection = false;
public Tag tagFromIntent = null;
private Button textRead;
private NFCForegroundUtil nfcForegroundUtil;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.project);
nfcForegroundUtil = new NFCForegroundUtil(this);
this.textRead= (Button) findViewById(R.id.button2);
initListeners();
}
private void initListeners() {
textRead.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if (connection == true)
{
DataDevice dataDevice = (DataDevice) getApplication();
dataDevice.setCurrentTag(tagFromIntent);
IsoDep nfca = IsoDep.get(dataDevice.getCurrentTag());
try
{
byte[] read= new byte[] { 0x00};
byte[] ans = null;
nfca.setTimeout(2000);
nfca.connect();
nfca.setTimeout(2000);
if (nfca.isConnected())
{
nfca.setTimeout(2000);
ans = nfca.transceive(read);
try
{
Thread.sleep(1500);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
nfca.close();
String textRead = HexBin.encode(ans);
}
catch (IOException e)
{
Log.i("A", "IOException is: " + e.getMessage());
e.printStackTrace();
}
if (nfca.isConnected())
{
try
{
nfca.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
});
}
#Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
action = intent.getAction();
tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
connection = true;
}
public void onPause()
{
super.onPause();
nfcForegroundUtil.disableForeground();
}
public void onResume()
{
super.onResume();
nfcForegroundUtil.enableForeground();
if (!nfcForegroundUtil.getNfc().isEnabled())
{
Toast.makeText(
getApplicationContext(),
"Please activate NFC and press Back to return to the application!",
Toast.LENGTH_LONG).show();
startActivity(new Intent(
android.provider.Settings.ACTION_WIRELESS_SETTINGS));
}
}
}
My manifest code:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.project"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.NFC" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:name="android.app.Application">
<activity
android:name=".StartActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.nfc.action.TECH_DISCOVERED" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:screenOrientation="portrait"
android:name=".Project"
android:label="#string/title_activity_main" />
</application>
My error:
FATAL EXCEPTION: main
java.lang.ClassCastException: android.app.Application cannot be cast to com.example.Project.DataDevice
at com.example.project.Project$1.onClick(Project.java:67)
at android.view.View.performClick(View.java:3511)
at android.view.View$PerformClick.run(View.java:14105)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
DataDevice class:
public class DataDevice extends Application
{
private Tag currentTag;
public void setCurrentTag(Tag currentTag) {
this.currentTag = currentTag;
}
public Tag getCurrentTag() {
return currentTag;
}
//(...)
}
I looked for answer on stackOverflow, nothing helped.
Anyone knows what's going on?
DataDevice and NFCUtilForeground works good (in other applications).
The android:name attribute in application tag in your manifest file should point to your DataDevice class. Like:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:name="your.package.DataDevice">
.........
..........
</application>