这一行代码:
setContentView(R.layout.main);
完成执行需要 2 到 7-8 秒,因为我在布局中添加了 Google Maps v2 片段,今天我在开发者控制台中收到了此 ANR:
DALVIK THREADS:
(mutexes: tll=0 tsl=0 tscl=0 ghl=0 hwl=0 hwll=0)
"main" prio=5 tid=1 SUSPENDED
| group="main" sCount=1 dsCount=0 obj=0x40022190 self=0xcec8
| sysTid=16482 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=-1345006400
| schedstat=( 602203379 1347412095 699 )
at java.lang.reflect.AccessibleObject.<init>(AccessibleObject.java:~132)
at java.lang.reflect.Method.<init>(Method.java:122)
at java.lang.Class.getDeclaredMethods(Native Method)
at java.lang.ClassCache.getDeclaredPublicMethods(ClassCache.java:153)
at java.lang.ClassCache.getMethodsRecursive(ClassCache.java:216)
at java.lang.ClassCache.findMethods(ClassCache.java:175)
at java.lang.ClassCache.getMethods(ClassCache.java:167)
at java.lang.Class.getMethod(Class.java:961)
at com.google.googlenav.capabilities.a.b((null):-1)
at maps.af.w.a((null):-1)
at maps.z.ab.a((null):-1)
at maps.z.ab.a((null):-1)
at maps.z.ag.a((null):-1)
at maps.z.ag.a((null):-1)
at maps.z.bw.a((null):-1)
at maps.z.r.onCreateView((null):-1)
at com.google.android.gms.maps.internal.IMapFragmentDelegate$Stub.onTransact(IMapFragmentDelegate.java:107)
at android.os.Binder.transact(Binder.java:279)
at com.google.android.gms.maps.internal.IMapFragmentDelegate$a$a.onCreateView((null):-1)
at com.google.android.gms.maps.SupportMapFragment$a.onCreateView((null):-1)
at com.google.android.gms.internal.e$4.a((null):-1)
at com.google.android.gms.internal.e.a((null):-1)
at com.google.android.gms.internal.e.onCreateView((null):-1)
at com.google.android.gms.maps.SupportMapFragment.onCreateView((null):-1)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:884)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1066)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1168)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:280)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:563)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:626)
at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
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:209)
at android.app.Activity.setContentView(Activity.java:1657)
at com.frakman.mapbook.MainActivity.onCreate(MainActivity.java:285)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
"Thread-13" prio=5 tid=10 TIMED_WAIT
| group="main" sCount=1 dsCount=0 obj=0x40557ef8 self=0x2249c8
| sysTid=16524 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=2247424
| schedstat=( 39337155 97991945 106 )
at java.lang.Object.wait(Native Method)
- waiting on <0x4055e098> (a com.google.ads.internal.c)
at java.lang.Object.wait(Object.java:395)
at com.google.ads.internal.c.run(SourceFile:758)
at java.lang.Thread.run(Thread.java:1019)
"Thread-12" prio=5 tid=11 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x40517058 self=0x21d3c0
| sysTid=16515 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=2217208
| schedstat=( 1129151 16143798 6 )
at android.os.BinderProxy.transact(Native Method)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:1881)
at android.app.ActivityThread.getProvider(ActivityThread.java:3351)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:3376)
at android.app.ContextImpl$ApplicationContentResolver.acquireProvider(ContextImpl.java:1689)
at android.content.ContentResolver.acquireProvider(ContentResolver.java:748)
at android.content.ContentResolver.query(ContentResolver.java:256)
at com.google.ads.as$a.run(SourceFile:75)
at java.lang.Thread.run(Thread.java:1019)
"CookieSyncManager" prio=5 tid=9 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x4056b3e8 self=0x1e7ef0
| sysTid=16512 nice=10 sched=0/0 cgrp=[fopen-error:2] handle=1990240
| schedstat=( 1007081 42510985 9 )
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:119)
at android.os.Looper.loop(Looper.java:117)
at android.webkit.WebSyncManager.run(WebSyncManager.java:90)
at android.webkit.CookieSyncManager.run(CookieSyncManager.java:61)
at java.lang.Thread.run(Thread.java:1019)
"WebViewCoreThread" prio=5 tid=8 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x40562ec8 self=0x1bc140
| sysTid=16510 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=1819256
| schedstat=( 56762704 347106931 100 )
at android.os.MessageQueue.nativePollOnce(Native Method)
at android.os.MessageQueue.next(MessageQueue.java:119)
at android.os.Looper.loop(Looper.java:117)
at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:685)
at java.lang.Thread.run(Thread.java:1019)
"Binder Thread #2" prio=5 tid=7 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x40513fa0 self=0x17ad48
| sysTid=16489 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=1499600
| schedstat=( 2532958 55328369 10 )
at dalvik.system.NativeStart.run(Native Method)
"Binder Thread #1" prio=5 tid=6 NATIVE
| group="main" sCount=1 dsCount=0 obj=0x40513260 self=0x16ebd8
| sysTid=16488 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=1499536
| schedstat=( 2868652 35583496 7 )
at dalvik.system.NativeStart.run(Native Method)
"Compiler" daemon prio=5 tid=5 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x40510498 self=0x16eaa0
| sysTid=16487 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=1424864
| schedstat=( 41778568 58563232 164 )
at dalvik.system.NativeStart.run(Native Method)
"Signal Catcher" daemon prio=5 tid=4 RUNNABLE
| group="system" sCount=0 dsCount=0 obj=0x405103d8 self=0x15b658
| sysTid=16485 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=1428136
| schedstat=( 8483884 45806889 24 )
at dalvik.system.NativeStart.run(Native Method)
"GC" daemon prio=5 tid=3 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x40510330 self=0x16dfd0
| sysTid=16484 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=1429376
| schedstat=( 99792479 377410890 75 )
at dalvik.system.NativeStart.run(Native Method)
"HeapWorker" daemon prio=5 tid=2 VMWAIT
| group="system" sCount=1 dsCount=0 obj=0x40510278 self=0x15bc20
| sysTid=16483 nice=0 sched=0/0 cgrp=[fopen-error:2] handle=1429632
| schedstat=( 4180908 8453369 17 )
at dalvik.system.NativeStart.run(Native Method)
可能与 setContentView 完成所需的长时间有关吗?有谁知道如何解决这个问题?提前致谢。
更新:这是我的 main.xml 布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#CCCCCC"
>
<RelativeLayout
android:id="@+id/main_ui_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#4099FF"
android:gravity="left"
android:orientation="vertical" >
<com.google.ads.AdView android:id="@+id/adView"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
ads:adUnitId="a151544472ef24f"
ads:adSize="SMART_BANNER"
ads:loadAdOnCreate="true"/>
<fragment
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@id/adView"
class="com.google.android.gms.maps.SupportMapFragment"/>
<RelativeLayout
android:id="@+id/bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
android:background="#4099FF">
<LinearLayout
android:id="@+id/layoutButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/spinner1"
android:layout_toLeftOf="@+id/authButton"
android:layout_centerHorizontal="true"
android:gravity="right"
android:orientation="horizontal"
>
<Button
android:id="@+id/checkin"
style="@style/NewButton.ButtonCheckin"
android:layout_width="50dp"
android:layout_height="50dp"
/>
<Button
android:id="@+id/query"
style="@style/NewButton.ButtonRefresh"
android:layout_width="50dp"
android:layout_height="50dp"
/>
</LinearLayout>
<com.facebook.widget.LoginButton
android:id="@+id/authButton"
android:layout_alignParentTop="true"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_marginRight="2dp"/>
<Spinner
android:id="@+id/spinner1"
android:layout_alignParentTop="true"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/layoutButtons"
/>
</RelativeLayout>
<ProgressBar
android:id="@+id/progressCircle"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:visibility="gone"
android:layout_centerVertical="true" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:visibility="gone"
android:layout_centerVertical="true" />
<ProgressBar
android:id="@+id/progressCircle2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/bar"
android:visibility="gone"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/adView"
android:layout_centerHorizontal="true"
android:orientation="horizontal">
<Button
android:id="@+id/settingsButton"
style="@style/NewButton.ButtonSettings"
android:layout_marginRight="5dp"
android:layout_width="45dp"
android:layout_height="45dp"
/>
<Button
android:id="@+id/searchButton"
style="@style/NewButton.ButtonSearch"
android:layout_marginLeft="5dp"
android:layout_width="45dp"
android:layout_height="45dp"
/>
<Button
android:id="@+id/infoButton"
style="@style/NewButton.ButtonInfo"
android:layout_marginLeft="5dp"
android:layout_width="45dp"
android:layout_height="45dp"
/>
</LinearLayout>
</RelativeLayout>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</FrameLayout>
</LinearLayout>
真正奇怪的行为是,这种缓慢的启动仅在您第一次启动应用程序时发生,或者如果您关闭它已经有几个小时了,而如果您使用后退按钮甚至最近的应用程序关闭它,那么它会在一秒钟内启动,很快,我真的不明白为什么。PS adMob 也是应用程序的一部分。
编辑:这里是 TraceView(这里是原始图像)