【问题标题】:Changing from 1 activity to another is really really slow从一项活动更改为另一项活动真的很慢
【发布时间】:2015-09-10 13:59:06
【问题描述】:

这是我使用的代码:

 Intent i = new Intent(PSTimelineActivity.this, PSPreCheckoutActivity.class);
                            i.putExtra("checkout", 0);
                            startActivity(i);

更改活动大约需要 5-6 秒。我的控制台日志被相同错误淹没的时间,数十次:

  09-10 15:56:16.661  31171-31189/nl.hgrams.passenger W/Binder﹕ Caught a RuntimeException from the binder stub implementation.
java.lang.NullPointerException: Attempt to read from field 'android.view.HardwareRenderer android.view.View$AttachInfo.mHardwareRenderer' on a null object reference
        at android.view.WindowManagerGlobal.dumpGfxInfo(WindowManagerGlobal.java:476)
        at android.app.ActivityThread$ApplicationThread.dumpGfxInfo(ActivityThread.java:1086)
        at android.app.ApplicationThreadNative.onTransact(ApplicationThreadNative.java:546)
        at android.os.Binder.execTransact(Binder.java:446)

TRACEVIEW 没有帮助,因为这些服务在应用程序的后台运行。 我在OnCreate()OnResume() 的开始和结束处为下一个活动添加了一些日志,它进入这里,记录它们,然后才被错误淹没。 View 只有在错误完成后才会出现在屏幕上。这很奇怪。因为在OnResume() 我应该已经看到了正确的观点?

 09-10 16:16:45.722  18674-18674/nl.hgrams.passenger I/﹕ BINDER here started activity
 09-10 16:16:45.798  18674-18674/nl.hgrams.passenger I/﹕ BINDER onCreate 1
 09-10 16:16:45.813  18674-18674/nl.hgrams.passenger I/﹕ BINDER onCreate 2
 09-10 16:16:45.819  18674-18674/nl.hgrams.passenger I/﹕ BINDER onResume 1
 09-10 16:16:45.836  18674-18674/nl.hgrams.passenger I/﹕ BINDER onResume 2
 09-10 16:16:45.914  18674-18857/nl.hgrams.passenger W/Binder﹕ Caught a RuntimeException from the binder stub implementation.
java.lang.NullPointerException: Attempt to read from field 'android.view.HardwareRenderer android.view.View$AttachInfo.mHardwareRenderer' on a null object reference
        at android.view.WindowManagerGlobal.dumpGfxInfo(WindowManagerGlobal.java:476)
        at android.app.ActivityThread$ApplicationThread.dumpGfxInfo(ActivityThread.java:1086)
        at android.app.ApplicationThreadNative.onTransact(ApplicationThreadNative.java:546)
        at android.os.Binder.execTransact(Binder.java:446)

错误可能在我的OnCreate() 函数中,甚至可能在单独的thread 上调用某些东西,这会导致错误。

这是我的OnCreate() 函数,如果有帮助的话: http://pastebin.com/1ajdzSr2

编辑:经过更多测试,我发现它挂在我从 OnResume() 方法调用的 init 函数上。 我的onResume()

@Override
protected void onResume() {
    super.onResume();
    CheckOutIntent cot = PSLocationCenter.getInstance().pref.getOutIntent(PSPreCheckoutActivity.this);
    if( cot != null){
        Intent intent = new Intent(PSPreCheckoutActivity.this, PSTimelineActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
        finish();
    }
    PSLocationCenter.getInstance().pref.setIsBackground(this, false);
    if(!PSLocationCenter.getInstance().mLocationClient.isConnected()) {
        PSLocationCenter.getInstance().startLocationClient();
    }
    init();
}

现在这是我的init() 功能: 我专门在 AsyncTask 中创建了它,因此它不会使用 UI 线程。 但是在里面,有一刻,我使用领域,它必须在主线程上,所以我发布了一个 Handler 帖子。这可能是问题吗?我还在调试,但我猜它可能在那里。 PS:去掉Asynctask会有帮助吗?

 public void init(){
    loader.setVisibility(View.VISIBLE);
    findViewById(R.id.subheader).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            closeSearchKeyboard();
        }
    });
    AsyncTask initAsync = new AsyncTask() {
        @Override
        protected Object doInBackground(Object[] params) {
            if(checkout == 2){
                Log.i("","PSPreCheckoutActivity checkout 2");
                if(PSLocationCenter.getInstance().mLocationClient.isConnected()){
                    try {
                        Geocoder geocoder = new Geocoder(PSPreCheckoutActivity.this, Locale.getDefault());
                        List<Address> address = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
                        if (address.size() > 0) {
                            if(tripName != null){
                                currentPlace = new Destination("google", null, null, null, tripName, loc.getLongitude(), loc.getLatitude() ,  (String.format(address.get(0).getMaxAddressLineIndex() > 0 ? address.get(0).getAddressLine(0) : "") + ", " + address.get(0).getLocality()), false, false, null);
                            }else{
                                currentPlace = new Destination("google", null, null, null, String.format(address.get(0).getMaxAddressLineIndex() > 0 ? address.get(0).getAddressLine(0) : ""), loc.getLongitude(), loc.getLatitude() ,  (String.format(address.get(0).getMaxAddressLineIndex() > 0 ? address.get(0).getAddressLine(0) : "") + ", " + address.get(0).getLocality()), false, false, null);
                            }
                        }
                        WillGetNearestData();
                    }catch (Exception e){
                        currentPlace = new Destination("nointerneterror", null, null, null, "nointerneterror", 0,0 , "nointerneterror", false, false, null);
                        WillGetNearestData();
                        Log.i("", "precheckout error: " + e.getMessage());
                        Utils.appendLog("precheckout error: " + e.getMessage());
                        if(e.getMessage().contains("Timed out")){
                            AlertDialog.show(PSPreCheckoutActivity.this, getString(R.string.Error), e.getMessage(), getString(R.string.OK), null);
                        }
                    }
                }else{
                    try {
                        PSLocationCenter.getInstance().startLocationClient();
                        Thread.sleep(1000);
                        init();
                    }catch (Exception e){
                        Log.e("", "precheckout init error is: " + e.getMessage());
                        if(e.getMessage().contains("Timed out")){
                            AlertDialog.show(PSPreCheckoutActivity.this, getString(R.string.Error), e.getMessage(), getString(R.string.OK), null);
                        }
                        init();
                    }

                }
            }else{
                Log.i("","PSPreCheckoutActivity checkout not 2");
                Handler han = new Handler(Looper.getMainLooper());
                han.post(new Runnable() {
                    @Override
                    public void run() {
                        PSTrip psTrip = PSTripDBFactory.getInstance(PSPreCheckoutActivity.this).getActiveTrip();
                        if(psTrip != null){
                            Destination destination = psTrip.getDestination();
                            LatLng finishLocation = new LatLng(PSLocationCenter.getInstance().locEnd.getLatitude(), PSLocationCenter.getInstance().locEnd.getLongitude());
                            if(destination != null) {
                                isRoaming = false;
                                finishLocation = new LatLng(psTrip.getDestination().getLat(), psTrip.getDestination().getLon());
                            }else{
                                isRoaming = true;
                            }
                            if(PSLocationCenter.getInstance().mLocationClient.isConnected()){
                                if(checkout != 2){
                                    loc = new Location("");
                                    loc.setLatitude(LocationServices.FusedLocationApi.getLastLocation(PSLocationCenter.getInstance().mLocationClient).getLatitude());
                                    loc.setLongitude(LocationServices.FusedLocationApi.getLastLocation(PSLocationCenter.getInstance().mLocationClient).getLongitude());
                                }
                                try {
                                    Geocoder geocoder = new Geocoder(PSPreCheckoutActivity.this, Locale.getDefault());
                                    List<Address> address = geocoder.getFromLocation(loc.getLatitude(), loc.getLongitude(), 1);
                                    if (address.size() > 0) {
                                        if(tripName != null){
                                            currentPlace = new Destination("google", null, null, null, tripName, loc.getLongitude(), loc.getLatitude() ,  (String.format(address.get(0).getMaxAddressLineIndex() > 0 ? address.get(0).getAddressLine(0) : "") + ", " + address.get(0).getLocality()), false, false, null);
                                        }else{
                                            currentPlace = new Destination("google", null, null, null, String.format(address.get(0).getMaxAddressLineIndex() > 0 ? address.get(0).getAddressLine(0) : ""), loc.getLongitude(), loc.getLatitude() ,  (String.format(address.get(0).getMaxAddressLineIndex() > 0 ? address.get(0).getAddressLine(0) : "") + ", " + address.get(0).getLocality()), false, false, null);
                                        }
                                    }
                                }catch (Exception e){
                                    Log.i("","precheckout error: " + e.getMessage());
                                    Utils.appendLog("precheckout error: " + e.getMessage());
                                }
                                LatLng locLatLng = new LatLng(loc.getLatitude(), loc.getLongitude());
                                if(Utils.distanceBetween2Points(locLatLng, finishLocation) < 250 && !isRoaming){ //TODO will have to be 250, now 9000 for hardcoded values to be seen all the time
                                    isNearby = true;
                                    plannedDestination = destination;
                                }
                                WillGetNearestData();
                            }else{
                                try {
                                    PSLocationCenter.getInstance().startLocationClient();
                                    Thread.sleep(1000);
                                    init();
                                }catch (Exception e){
                                    Log.e("","precheckout init 2 error is: " + e.getMessage());
                                    init();
                                }

                            }
                        }
                    }
                });
            }
            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
        }
    };
    initAsync.execute();
}

【问题讨论】:

  • “更改活动大约需要 5-6 秒” -- 使用 Traceview 确定问题所在。
  • 您在使用服务吗?我说是因为你的踪迹中提到的活页夹。请记住,服务在主线程上运行...
  • 是的,我有在后台运行的服务。但是我对其他活动有其他意图,这些活动效果很好,从一个到另一个的过渡没有问题。 @CommonsWare 我尝试使用 TraceView 但是,在后台提供服务,它们消耗了我的大部分 CPU 内存,我不知道这是什么错误原因:) 并且导致其他活动的变化要快得多,我认为服务不是问题
  • @rosual 在布局遍历中,最终导致屏幕上显示的内容与活动生命周期方法异步发生,即仅仅因为onResumefinished 并不意味着还显示任何内容。您可能需要展示更多 PSPreCheckoutActivity 类。
  • 你见过this吗?

标签: android android-intent android-activity android-studio start-activity


【解决方案1】:

感谢@Ci_ 我发现了这个问题。这显然是 Android Studio 1.4 Preview 的一个错误。或 Android SDK。 有关更多详细信息,请查看此处: Android: Constant memory consumption / dumpGfxInfo() 这帮助我解决了它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-03
    • 2014-09-19
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多