I try to get the phone number of my device, get the following error:
FATAL EXCEPTION:
main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dell.myapplication/com.example.dell.myapplication.MainActivity}: java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10035 nor current process has android.permission.READ_PHONE_STATE.
I am a new at android development. I have looked at the errors that have been generated by LogCat but do not know the problem and where to find it based on the errors I received.
Thanks for your help and sorry for the duration!
here is my code:
MainActivity.java
package com.example.dell.myapplication;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.List;
public class MainActivity extends ActionBarActivity {
public MainActivity() {
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TelephonyManager tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String imei = tm.getDeviceId();
String phone = tm.getLine1Number();
Toast.makeText(MainActivity.this, "my IMEI: "+imei, Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this,"my phone: "+phone,Toast.LENGTH_LONG).show();
}
#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_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();
//noinspection SimplifiableIfStatement
// if (id == R.id.action_settings) {
// return true;
// }
return super.onOptionsItemSelected(item);
}
}
activity_main.xml
<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=".MainActivity">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="121dp" />
</RelativeLayout>
LogCat
12-23 12:50:35.013 8789-8789/? D/dalvikvm﹕ Late-enabling CheckJNI
12-23 12:50:35.303 8789-8789/com.example.dell.myapplication I/dalvikvm﹕ Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
12-23 12:50:35.303 8789-8789/com.example.dell.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 13332: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
12-23 12:50:35.303 8789-8789/com.example.dell.myapplication D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0007
12-23 12:50:35.308 8789-8789/com.example.dell.myapplication I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
12-23 12:50:35.308 8789-8789/com.example.dell.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 408: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
12-23 12:50:35.308 8789-8789/com.example.dell.myapplication D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
12-23 12:50:35.308 8789-8789/com.example.dell.myapplication I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
12-23 12:50:35.308 8789-8789/com.example.dell.myapplication W/dalvikvm﹕ VFY: unable to resolve virtual method 430: Landroid/content/res/TypedArray;.getType (I)I
12-23 12:50:35.308 8789-8789/com.example.dell.myapplication D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
12-23 12:50:35.443 8789-8789/com.example.dell.myapplication D/AndroidRuntime﹕ Shutting down VM
12-23 12:50:35.443 8789-8789/com.example.dell.myapplication W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x416f02a0)
12-23 12:50:35.468 8789-8789/com.example.dell.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dell.myapplication/com.example.dell.myapplication.MainActivity}: java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10035 nor current process has android.permission.READ_PHONE_STATE.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
at android.app.ActivityThread.access$700(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
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:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10035 nor current process has android.permission.READ_PHONE_STATE.
at android.os.Parcel.readException(Parcel.java:1425)
at android.os.Parcel.readException(Parcel.java:1379)
at com.android.internal.telephony.IPhoneSubInfo$Stub$Proxy.getDeviceId(IPhoneSubInfo.java:222)
at android.telephony.TelephonyManager.getDeviceId(TelephonyManager.java:249)
at com.example.dell.myapplication.MainActivity.onCreate(MainActivity.java:37)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
at android.app.ActivityThread.access$700(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
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:1027)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
at dalvik.system.NativeStart.main(Native Method)
12-23 12:50:35.513 8789-8793/com.example.dell.myapplication D/dalvikvm﹕ GC_CONCURRENT freed 234K, 14% free 9604K/11143K, paused 3ms+3ms, total 49ms
12-23 12:50:48.168 8789-8789/com.example.dell.myapplication I/Process﹕ Sending signal. PID: 8789 SIG: 9
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dell.myapplication" >
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/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>
</application>
</manifest>
Add READ_PHONE_STATE permission
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
in your manifest file
READ_PHONE_STATE
Allows read only access to phone state.
Note: If both your minSdkVersion and targetSdkVersion values are set
to 3 or lower, the system implicitly grants your app this permission.
If you don't need this permission, be sure your targetSdkVersion is 4
or higher.
Protection level: dangerous
Constant Value:
"android.permission.READ_PHONE_STATE"
in your manifest, you need to add followong permission in order to read phone number:
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
In your logCat, system tells the you the reason of the error with this line:
Caused by: java.lang.SecurityException: Requires READ_PHONE_STATE: Neither user 10035 nor current process has android.permission.READ_PHONE_STATE.
Add Permission READ_PHONE_STATE
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
I have this problem and figure out that you need to add READ_PHONE_NUMBERS if your targetSdkVersion is 31
<uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
You need to request that permission explicitly also
Related
I am trying to install an admob ad on my webview app that I developed with Android Studio but the app keeps crashing now and I can't seem to get the code right.... everywhere I read about the Android SDK is different information....
This is the error I keep getting and I can't seem to figure out why I am getting it..
03-17 02:31:31.637 15034-15034/? I/SELinux: Function: selinux_android_load_priority [0], There is no sepolicy file.
03-17 02:31:31.697 15034-15034/? I/SELinux: Function: selinux_android_load_priority , spota verifySig and checkHash pass. priority version is VE=SEPF_SGH-I527M_4.4.2_0046
03-17 02:31:31.697 15034-15034/? I/SELinux: selinux_android_seapp_context_reload: seapp_contexts file is loaded from /data/security/spota/seapp_contexts
03-17 02:31:31.697 15034-15034/? D/dalvikvm: Late-enabling CheckJNI
03-17 02:31:32.127 15034-15034/com.payforlikes.app.payforlikes I/dalvikvm-heap: Grow heap (frag case) to 23.093MB for 14791732-byte allocation
03-17 02:31:32.278 15034-15034/com.payforlikes.app.payforlikes I/Adreno-EGL: <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build: (CL3869936) OpenGL ES Shader Compiler Version: 17.01.12.SPL Build Date: 03/03/14 Mon Local Branch: default Remote Branch: Local Patches: Reconstruct Branch:
03-17 02:31:32.428 15034-15034/com.payforlikes.app.payforlikes D/OpenGLRenderer: Enabling debug mode 0
03-17 02:31:33.339 15034-15034/com.payforlikes.app.payforlikes E/OpenGLRenderer: SFEffectCache:clear(), mSize = 0
03-17 02:31:35.271 15034-15034/com.payforlikes.app.payforlikes I/dalvikvm: Could not find method android.view.Window$Callback.onProvideKeyboardShortcuts, referenced from method android.support.v7.view.WindowCallbackWrapper.onProvideKeyboardShortcuts
03-17 02:31:35.271 15034-15034/com.payforlikes.app.payforlikes W/dalvikvm: VFY: unable to resolve interface method 19659: Landroid/view/Window$Callback;.onProvideKeyboardShortcuts (Ljava/util/List;Landroid/view/Menu;)V
03-17 02:31:35.271 15034-15034/com.payforlikes.app.payforlikes D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
03-17 02:31:35.271 15034-15034/com.payforlikes.app.payforlikes W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
03-17 02:31:35.271 15034-15034/com.payforlikes.app.payforlikes I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
03-17 02:31:35.271 15034-15034/com.payforlikes.app.payforlikes W/dalvikvm: VFY: unable to resolve interface method 19661: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
03-17 02:31:35.271 15034-15034/com.payforlikes.app.payforlikes D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
03-17 02:31:35.271 15034-15034/com.payforlikes.app.payforlikes I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
03-17 02:31:35.271 15034-15034/com.payforlikes.app.payforlikes W/dalvikvm: VFY: unable to resolve interface method 19665: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
03-17 02:31:35.271 15034-15034/com.payforlikes.app.payforlikes D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
03-17 02:31:35.271 15034-15034/com.payforlikes.app.payforlikes I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
03-17 02:31:35.271 15034-15034/com.payforlikes.app.payforlikes W/dalvikvm: VFY: unable to resolve virtual method 545: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
03-17 02:31:35.271 15034-15034/com.payforlikes.app.payforlikes D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
03-17 02:31:35.271 15034-15034/com.payforlikes.app.payforlikes I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
03-17 02:31:35.271 15034-15034/com.payforlikes.app.payforlikes W/dalvikvm: VFY: unable to resolve virtual method 567: Landroid/content/res/TypedArray;.getType (I)I
03-17 02:31:35.271 15034-15034/com.payforlikes.app.payforlikes D/dalvikvm: VFY: replacing opcode 0x6e at 0x0008
03-17 02:31:35.351 15034-15034/com.payforlikes.app.payforlikes V/WebViewChromium: Binding Chromium to the background looper Looper (main, tid 1) {41ea0198}
03-17 02:31:35.391 15034-15034/com.payforlikes.app.payforlikes I/chromium: [INFO:library_loader_hooks.cc(112)] Chromium logging enabled: level = 0, default verbosity = 0
03-17 02:31:35.391 15034-15034/com.payforlikes.app.payforlikes I/BrowserProcessMain: Initializing chromium process, renderers=0
03-17 02:31:35.521 15034-15034/com.payforlikes.app.payforlikes I/Adreno-EGL: <qeglDrvAPI_eglInitialize:381>: EGL 1.4 QUALCOMM build: (CL3869936) OpenGL ES Shader Compiler Version: 17.01.12.SPL Build Date: 03/03/14 Mon Local Branch: default Remote Branch: Local Patches: Reconstruct Branch:
03-17 02:31:35.531 15034-15257/com.payforlikes.app.payforlikes W/chromium: [WARNING:proxy_service.cc(888)] PAC support disabled because there is no system implementation
03-17 02:31:35.731 15034-15034/com.payforlikes.app.payforlikes W/Ads: Required XML attribute "adSize" was missing.
03-17 02:31:35.992 15034-15034/com.payforlikes.app.payforlikes D/AndroidRuntime: Shutting down VM
03-17 02:31:35.992 15034-15034/com.payforlikes.app.payforlikes W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x416cdda0)
03-17 02:31:36.002 15034-15034/com.payforlikes.app.payforlikes E/AndroidRuntime: FATAL EXCEPTION: main Process: com.payforlikes.app.payforlikes, PID: 15034 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.payforlikes.app.payforlikes/com.payforlikes.app.payforlikes.MainActivity}: java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: The ad size and ad unit ID must be set before loadAd is called.
at com.google.android.gms.ads.internal.client.zzab.zzdg(Unknown Source)
at com.google.android.gms.ads.internal.client.zzab.zza(Unknown Source)
at com.google.android.gms.ads.BaseAdView.loadAd(Unknown Source)
at com.google.android.gms.ads.AdView.loadAd(Unknown Source)
at com.payforlikes.app.payforlikes.MainActivity.onCreate(MainActivity.java:55)
at android.app.Activity.performCreate(Activity.java:5451)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Here is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.payforlikes.app.payforlikes.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
Here is my build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.payforlikes.app.payforlikes"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
} }
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0-alpha1'
compile 'com.android.support:design:24.0.0-alpha1'
compile 'com.google.android.gms:play-services-ads:8.4.0'
}
Here is my MainActivity.java
package com.payforlikes.app.payforlikes;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd; public class MainActivity extends AppCompatActivity {
InterstitialAd mInterstitialAd;
private InterstitialAd interstitial;
private static final String TEST_DEVICE_ID = "INSERT_YOUR_TEST_DEVICE_ID_HERE";
private WebView mWebView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.setWebViewClient(new WebViewClient());
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://payforlikes.com/");
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// The "loadAdOnCreate" and "testDevices" XML attributes no longer available.
AdView adView = (AdView) this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(TEST_DEVICE_ID)
.build();
adView.loadAd(adRequest);
}
#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_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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void displayInterstitial() { // If Ads are loaded, show Interstitial else show nothing.
if (interstitial.isLoaded()) {
interstitial.show();
}
} }
Here is my content_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.payforlikes.app.payforlikes.MainActivity"
tools:showIn="#layout/activity_main">
<WebView
android:id="#+id/activity_main_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.gms.ads.AdView android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="INSERT_YOUR_AD_UNIT_ID_HERE"/> </RelativeLayout>
Here is my AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
package="com.payforlikes.app.payforlikes">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".SplashScreen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
I'm developing an application that makes use of the Google Maps API. However, when I run the application on an emulator, I get the following error: "Failed to load map module."
I made sure I checked "Google Maps API For Android" from the Developer's Console. The following are my configurations:
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="MYPACKAGE">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<permission android:name="MYPACKAGE.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="MY_API_KEY"/>
<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>
Where MY_API_KEY is the actual API key I received from Google.
activity_main.xml:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="MYPACKAGE.MainActivity" />
MainActivity.java:
package MYPACKAGE;
import android.graphics.Color;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.Circle;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
The LogCat shows me the following when I attempt to run the code:
02-12 03:57:57.370 1261-1261/? D/dalvikvm: Not late-enabling CheckJNI (already on)
02-12 03:57:59.030 1261-1261/com.be2015.meetupmap I/dalvikvm: Could not find method android.app.Notification$Builder.setLocalOnly, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zza
02-12 03:57:59.040 1261-1261/com.be2015.meetupmap W/dalvikvm: VFY: unable to resolve virtual method 240: Landroid/app/Notification$Builder;.setLocalOnly (Z)Landroid/app/Notification$Builder;
02-12 03:57:59.040 1261-1261/com.be2015.meetupmap D/dalvikvm: VFY: replacing opcode 0x6e at 0x00c8
02-12 03:57:59.090 1261-1261/com.be2015.meetupmap I/dalvikvm: Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.GooglePlayServicesUtil.zzh
02-12 03:57:59.090 1261-1261/com.be2015.meetupmap W/dalvikvm: VFY: unable to resolve virtual method 542: Landroid/content/pm/PackageManager;.getPackageInstaller ()Landroid/content/pm/PackageInstaller;
02-12 03:57:59.100 1261-1261/com.be2015.meetupmap D/dalvikvm: VFY: replacing opcode 0x6e at 0x000b
02-12 03:57:59.520 1261-1293/com.be2015.meetupmap I/GMPM: App measurement is starting up
02-12 03:57:59.680 1261-1293/com.be2015.meetupmap E/GMPM: getGoogleAppId failed with status: 10
02-12 03:57:59.700 1261-1293/com.be2015.meetupmap E/GMPM: Uploading is not possible. App measurement disabled
02-12 03:58:00.240 1261-1261/com.be2015.meetupmap D/dalvikvm: GC_FOR_ALLOC freed 338K, 12% free 3048K/3452K, paused 171ms, total 213ms
02-12 03:58:00.250 1261-1261/com.be2015.meetupmap I/zzy: Making Creator dynamically
02-12 03:58:01.520 1261-1261/com.be2015.meetupmap I/dalvikvm: Could not find method android.app.Notification$Builder.setLocalOnly, referenced from method com.google.android.gms.common.mz.b
02-12 03:58:01.520 1261-1261/com.be2015.meetupmap W/dalvikvm: VFY: unable to resolve virtual method 141: Landroid/app/Notification$Builder;.setLocalOnly (Z)Landroid/app/Notification$Builder;
02-12 03:58:01.520 1261-1261/com.be2015.meetupmap D/dalvikvm: VFY: replacing opcode 0x6e at 0x00ce
02-12 03:58:01.530 1261-1261/com.be2015.meetupmap I/dalvikvm: Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.nb.a
02-12 03:58:01.530 1261-1261/com.be2015.meetupmap W/dalvikvm: VFY: unable to resolve virtual method 424: Landroid/content/pm/PackageManager;.getPackageInstaller ()Landroid/content/pm/PackageInstaller;
02-12 03:58:01.530 1261-1261/com.be2015.meetupmap D/dalvikvm: VFY: replacing opcode 0x6e at 0x000d
02-12 03:58:01.630 1261-1261/com.be2015.meetupmap D/ChimeraCfgMgr: Reading stored module config
02-12 03:58:01.680 1261-1261/com.be2015.meetupmap W/DynamiteUtils: Failed to load module: module id not found
02-12 03:58:01.680 1261-1261/com.be2015.meetupmap W/DynamiteUtils: Attempt 1 failed.
02-12 03:58:02.270 1261-1261/com.be2015.meetupmap W/DynamiteUtils: Failed to load module: module id not found
02-12 03:58:02.270 1261-1261/com.be2015.meetupmap W/DynamiteUtils: Attempt 2 failed.
02-12 03:58:02.370 1261-1261/com.be2015.meetupmap W/DynamiteUtils: Failed to load module: module id not found
02-12 03:58:02.370 1261-1261/com.be2015.meetupmap W/DynamiteUtils: Attempt 3 failed.
02-12 03:58:02.420 1261-1261/com.be2015.meetupmap D/dalvikvm: DexOpt: couldn't find static field Landroid/os/Build;.SUPPORTED_64_BIT_ABIS
02-12 03:58:02.430 1261-1261/com.be2015.meetupmap W/dalvikvm: VFY: unable to resolve static field 119 (SUPPORTED_64_BIT_ABIS) in Landroid/os/Build;
02-12 03:58:02.440 1261-1261/com.be2015.meetupmap D/dalvikvm: VFY: replacing opcode 0x62 at 0x0012
02-12 03:58:02.440 1261-1261/com.be2015.meetupmap D/dalvikvm: DexOpt: couldn't find static field Landroid/os/Build;.SUPPORTED_32_BIT_ABIS
02-12 03:58:02.450 1261-1261/com.be2015.meetupmap W/dalvikvm: VFY: unable to resolve static field 118 (SUPPORTED_32_BIT_ABIS) in Landroid/os/Build;
02-12 03:58:02.450 1261-1261/com.be2015.meetupmap D/dalvikvm: VFY: replacing opcode 0x62 at 0x0021
02-12 03:58:02.470 1261-1261/com.be2015.meetupmap D/dalvikvm: DexOpt: couldn't find static field Landroid/os/Build;.SUPPORTED_64_BIT_ABIS
02-12 03:58:02.470 1261-1261/com.be2015.meetupmap W/dalvikvm: VFY: unable to resolve static field 119 (SUPPORTED_64_BIT_ABIS) in Landroid/os/Build;
02-12 03:58:02.470 1261-1261/com.be2015.meetupmap D/dalvikvm: VFY: replacing opcode 0x62 at 0x0008
02-12 03:58:02.470 1261-1261/com.be2015.meetupmap D/dalvikvm: DexOpt: couldn't find static field Landroid/os/Build;.SUPPORTED_ABIS
02-12 03:58:02.480 1261-1261/com.be2015.meetupmap W/dalvikvm: VFY: unable to resolve static field 120 (SUPPORTED_ABIS) in Landroid/os/Build;
02-12 03:58:02.480 1261-1261/com.be2015.meetupmap D/dalvikvm: VFY: replacing opcode 0x62 at 0x0008
02-12 03:58:02.480 1261-1261/com.be2015.meetupmap W/DynamiteUtils: Client module failure reason: 11
02-12 03:58:02.510 1261-1261/com.be2015.meetupmap D/maps.CreatorImpl: Module could not be loaded.
02-12 03:58:02.570 1261-1261/com.be2015.meetupmap D/maps.CreatorImpl: Failed to load maps module.
android.os.RemoteException
at com.google.android.gms.maps.internal.CreatorImpl.a(SourceFile:76)
at com.google.android.gms.maps.internal.CreatorImpl.b(SourceFile:96)
at com.google.android.gms.maps.internal.ag.onTransact(SourceFile:62)
at android.os.Binder.transact(Binder.java:361)
at com.google.android.gms.maps.internal.zzc$zza$zza.zzt(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment$zzb.zzzh(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment$zzb.zza(Unknown Source)
at com.google.android.gms.dynamic.zza.zza(Unknown Source)
at com.google.android.gms.dynamic.zza.onInflate(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
at android.support.v4.app.Fragment.onInflate(Fragment.java:1129)
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2283)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:111)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:314)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:31)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:690)
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:290)
at android.app.Activity.setContentView(Activity.java:1929)
at com.be2015.meetupmap.MainActivity.onCreate(MainActivity.java:23)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
02-12 03:58:03.310 1261-1261/com.be2015.meetupmap W/EGL_emulation: eglSurfaceAttrib not implemented
02-12 03:58:03.350 1261-1261/com.be2015.meetupmap D/OpenGLRenderer: Enabling debug mode 0
Can anyone tell me what's going wrong? I have absolutely no idea!
Maybe this is your problem?!
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) {
GooglePlayServicesUtil.getErrorDialog(resultCode, this,
PLAY_SERVICES_RESOLUTION_REQUEST).show();
} else {
Log.i(TAG, "This device is not supported.");
finish();
}
}
Android - Google Map v2 - need update google play service on device
I am using Andoid Studio to develop an app and while opening the app , I am not able to see my web page ,a white screen is coming. I saw the console and find this error
E/Trace﹕ error opening trace file: No such file or directory (2)
When I see my manifest file, its seems fine for me and also MainActivity.java seems good for me.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pe.com.bcp.creaapp" >
<application
android:allowBackup="true"
android:icon="#mipmap/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>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
Here is the java class
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import java.io.File;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.myWebView);
//myWebView.getSettings().setJavaScriptEnabled(true);
String filePath = "file:///android_asset/Flickular-Demo-master/src/index.html";
myWebView.loadUrl(filePath);
}
#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_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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I do not understand why Its not showing and what I am missing ?
Activity Main Layout.
<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=".MainActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myWebView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Below is the log from Console :-
09-04 13:38:20.307 10194-10194/ D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
09-04 13:38:20.318 10194-10194/ E/Trace﹕ error opening trace file: No such file or directory (2)
09-04 13:38:20.327 10194-10194/ W/dalvikvm﹕ VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
09-04 13:38:20.327 10194-10194/ I/dalvikvm﹕ Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onSearchRequested
09-04 13:38:20.327 10194-10194/ W/dalvikvm﹕ VFY: unable to resolve interface method 14035: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
09-04 13:38:20.327 10194-10194/ D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002
09-04 13:38:20.327 10194-10194/ I/dalvikvm﹕ Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onWindowStartingActionMode
09-04 13:38:20.327 10194-10194/ W/dalvikvm﹕ VFY: unable to resolve interface method 14039: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
09-04 13:38:20.327 10194-10194/ D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002
09-04 13:38:20.327 10194-10194/ I/dalvikvm﹕ Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged
09-04 13:38:20.327 10194-10194/ W/dalvikvm﹕ VFY: unable to resolve virtual method 13936: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V
09-04 13:38:20.327 10194-10194/ D/dalvikvm﹕ VFY: replacing opcode 0x6f at 0x0007
09-04 13:38:20.327 10194-10194/ I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
09-04 13:38:20.327 10194-10194/ W/dalvikvm﹕ VFY: unable to resolve virtual method 401:
Landroid/content/res/TypedArray;.getChangingConfigurations ()I
09-04 13:38:20.327 10194-10194/ D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
09-04 13:38:20.327 10194-10194/ I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
09-04 13:38:20.327 10194-10194/ W/dalvikvm﹕ VFY: unable to resolve virtual method 423: Landroid/content/res/TypedArray;.getType (I)I
09-04 13:38:20.327 10194-10194/ D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
09-04 13:38:20.379 10194-10199 D/dalvikvm﹕ GC_CONCURRENT freed 182K, 3% free 10944K/11207K, paused 15ms+0ms, total 17ms
09-04 13:38:20.928 10194-10201/ I/jdwp﹕ Ignoring second debugger -- accepting and dropping
09-04 13:38:21.233 10194-10194/ I/Choreographer﹕ Skipped 44 frames! The application may be doing too much work on its main thread.
09-04 13:38:21.233 10194-10194/ D/gralloc_goldfish﹕ Emulator without GPU emulation detected.
I tried following the tutorial from Google here. I believe I followed every instruction written but I still get nullpointexception when I try to get my long and lat coordinates.
It works fine if I press the target icon in the map that redirects me to my current location. But when I try it programmatically, it returns nullpointexception. I tried looking in stackoverflow for similar situations like mine but so far none of them works.
If you can provide me with a sample working source file or code that I can test myself it is much appreciated. Below are my source code in case I missed something. Thanks in advance.
manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sample.map.activity"
android:versionCode="1"
android:versionName="1.0" >
<supports-screens
android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="false"
android:xlargeScreens="true" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.CAMERA" />
<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="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" />
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="19" />
<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" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name="sample.map.activity"
android:configChanges="orientation"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<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="AIzaSyAdqod8BHch40COYn19c6Ds0uhyfkX25SA" />
</application>
</manifest>
map activity class
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
public class MapActivity extends ActionBarActivity {
GoogleMap map;
Location location;
LatLng myLocation;
LocationManager lm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String provider = lm.getBestProvider(criteria, true);
map = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
map.setMyLocationEnabled(true);
location = lm.getLastKnownLocation(provider);
double mLat = location.getLatitude();
double mLong = location.getLongitude();
Toast.makeText(getApplicationContext(), mLat+" "+mLong, Toast.LENGTH_LONG).show();
map.animateCamera(CameraUpdateFactory.newLatLngZoom(myLocation, 13));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.map, 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);
}
}
Here is the stack trace
05-18 22:45:44.043: E/AndroidRuntime(1220): FATAL EXCEPTION: main
05-18 22:45:44.043: E/AndroidRuntime(1220): Process: sample.map.activity, PID: 1220
05-18 22:45:44.043: E/AndroidRuntime(1220): java.lang.RuntimeException: Unable to start activity ComponentInfo{sample.map.activity/sample.map.activity.MapActivity}: java.lang.NullPointerException
05-18 22:45:44.043: E/AndroidRuntime(1220): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-18 22:45:44.043: E/AndroidRuntime(1220): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-18 22:45:44.043: E/AndroidRuntime(1220): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-18 22:45:44.043: E/AndroidRuntime(1220): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-18 22:45:44.043: E/AndroidRuntime(1220): at android.os.Handler.dispatchMessage(Handler.java:102)
05-18 22:45:44.043: E/AndroidRuntime(1220): at android.os.Looper.loop(Looper.java:136)
05-18 22:45:44.043: E/AndroidRuntime(1220): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-18 22:45:44.043: E/AndroidRuntime(1220): at java.lang.reflect.Method.invokeNative(Native Method)
05-18 22:45:44.043: E/AndroidRuntime(1220): at java.lang.reflect.Method.invoke(Method.java:515)
05-18 22:45:44.043: E/AndroidRuntime(1220): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-18 22:45:44.043: E/AndroidRuntime(1220): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-18 22:45:44.043: E/AndroidRuntime(1220): at dalvik.system.NativeStart.main(Native Method)
05-18 22:45:44.043: E/AndroidRuntime(1220): Caused by: java.lang.NullPointerException
05-18 22:45:44.043: E/AndroidRuntime(1220): at sample.map.activity.MapActivity.onCreate(MapActivity.java:39)
05-18 22:45:44.043: E/AndroidRuntime(1220): at android.app.Activity.performCreate(Activity.java:5231)
05-18 22:45:44.043: E/AndroidRuntime(1220): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-18 22:45:44.043: E/AndroidRuntime(1220): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-18 23:04:59.383: W/ActivityThread(1354): ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());
05-18 23:04:59.813: I/Process(1354): Sending signal. PID: 1354 SIG: 9
05-18 23:05:01.123: D/dalvikvm(1401): GC_FOR_ALLOC freed 89K, 6% free 2960K/3128K, paused 26ms, total 28ms
05-18 23:05:01.123: I/dalvikvm-heap(1401): Grow heap (frag case) to 3.569MB for 635812-byte allocation
05-18 23:05:01.163: D/dalvikvm(1401): GC_FOR_ALLOC freed 2K, 5% free 3578K/3752K, paused 32ms, total 32ms
05-18 23:05:01.253: W/Visite Dates(1401): Reselected 0 tab name Unposted
05-18 23:05:01.463: D/(1401): HostConnection::get() New Host Connection established 0xb82b7f80, tid 1401
05-18 23:05:01.513: W/EGL_emulation(1401): eglSurfaceAttrib not implemented
05-18 23:05:01.523: D/OpenGLRenderer(1401): Enabling debug mode 0
05-18 23:51:25.343: W/EGL_emulation(1401): eglSurfaceAttrib not implemented
05-18 23:54:27.943: W/EGL_emulation(1401): eglSurfaceAttrib not implemented
Here is the xml file of my layout.
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
I dont know what you have done in your xml file,but your xml file must look like this:
activity_main.xml
<?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>
Also Add the following,
< meta-data android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" >
< /meta-data>
in your manifest file immediately below the first meta-data tag(where you have placed your google api key).
Also I havent used the camera code that you have written to animate camera.My code:
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(your_lat, your_long)).zoom(12).build();
map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Finally got it to work. It seems that the problem is the network provider.
Instead of using Criteria and getBestProvider I followed this approach. What it does is loop to a list of network provider and returns the best existing provider in your mobile phone manually.
i'm making an android map application , using goggle maps v2 , alla went ok until i run the application , it crashs . thes are my files :
MainActivity.java
package com.geo.app;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity {
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
private GoogleMap map;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = map.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool")
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher)));
// Move the camera instantly to hamburg with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
}
activity_main.xml
<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"
tools:context=".MainActivity" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.geo.app"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="18" />
<permission
android:name="com.geo.app.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<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="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two 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="com.geo.app.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="AIzaSyB0MLq8L4YCA9GC05iD1V7OYfqlkUa0lhA" />
</application>
</manifest>
The application crashed , and i have this on my LogCat
04-10 10:59:02.545 11918-11918/? D/AndroidRuntime﹕ >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
04-10 10:59:02.545 11918-11918/? D/AndroidRuntime﹕ CheckJNI is OFF
04-10 10:59:02.725 11918-11918/? D/AndroidRuntime﹕ --- registering native functions ---
04-10 10:59:03.495 104-205/? I/ActivityManager﹕ Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.geo.app/.MainActivity }
04-10 10:59:03.605 11918-11918/? D/AndroidRuntime﹕ Shutting down VM
04-10 10:59:03.605 11918-11923/? D/dalvikvm﹕ Debugger has detached; object registry had 1 entries
04-10 10:59:03.625 11918-11926/? I/AndroidRuntime﹕ NOTE: attach of thread 'Binder Thread #3' failed
04-10 10:59:03.675 5325-5325/? D/PackageAddedReceiver﹕ package added com.geo.app
04-10 10:59:03.695 104-107/? I/ActivityManager﹕ Start proc com.geo.app for activity com.geo.app/.MainActivity: pid=11928 uid=10082 gids={3003, 1015}
04-10 10:59:04.175 182-182/? D/dalvikvm﹕ GC_EXPLICIT freed 5596 objects / 283440 bytes in 5136ms
04-10 10:59:04.275 3332-11910/? D/RegisterService﹕ insert plugin size 1
04-10 10:59:04.335 104-243/? D/Sensors﹕ open_akm, fd=148
04-10 10:59:04.625 77-77/? D/AK8973﹕ Compass Start
04-10 10:59:04.745 11928-11928/com.geo.app W/dalvikvm﹕ VFY: unable to resolve static field 3704 (MapAttrs) in Lcom/google/android/gms/R$styleable;
04-10 10:59:04.745 11928-11928/com.geo.app D/dalvikvm﹕ VFY: replacing opcode 0x62 at 0x000e
04-10 10:59:04.745 11928-11928/com.geo.app D/dalvikvm﹕ VFY: dead code 0x0010-00ae in Lcom/google/android/gms/maps/GoogleMapOptions;.createFromAttributes (Landroid/content/Context;Landroid/util/AttributeSet;)Lcom/google/android/gms/maps/GoogleMapOptions;
04-10 10:59:04.755 11928-11928/com.geo.app D/AndroidRuntime﹕ Shutting down VM
04-10 10:59:04.755 11928-11928/com.geo.app W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40028a00)
04-10 10:59:04.775 11928-11928/com.geo.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(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:558)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
at android.app.Activity.setContentView(Activity.java:1654)
at com.geo.app.MainActivity.onCreate(MainActivity.java:26)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1065)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2745)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797)
at android.app.ActivityThread.access$2300(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4914)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
04-10 10:59:04.845 104-244/? W/ActivityManager﹕ Force finishing activity com.geo.app/.MainActivity
You are missing a meta tag in the manifest file
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
You don't need these
<permission
android:name="com.geo.app.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
Also it is better to check the availability of google play services before intializing GoogleMap object
Edit:
java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
You haven't referenced google play services library project in your android map project.
In your MainActivity.java class change the fragment import statement
import android.support.v4.app.Fragment