从 1 activity 更改为另一个真的很慢
Changing from 1 activity to another is really really slow
这是我使用的代码:
Intent i = new Intent(PSTimelineActivity.this, PSPreCheckoutActivity.class);
i.putExtra("checkout", 0);
startActivity(i);
更改 activity 大约需要 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 没有帮助,因为在应用程序后台运行的服务。
我为下一个 Activity 添加了一些日志,在 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 线程。
但是在里面,有一刻,我使用了必须在主线程上的领域,所以我做了一个处理程序post。这可能是问题所在吗?我还在调试,但我猜它可能在那里。
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();
}
感谢@Ci_ 我发现了这个问题。这显然是 Android Studio 1.4 Preview 的错误。或 Android SDK。
有关详细信息,请查看此处:
这帮助我解决了问题
这是我使用的代码:
Intent i = new Intent(PSTimelineActivity.this, PSPreCheckoutActivity.class);
i.putExtra("checkout", 0);
startActivity(i);
更改 activity 大约需要 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 没有帮助,因为在应用程序后台运行的服务。
我为下一个 Activity 添加了一些日志,在 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 线程。
但是在里面,有一刻,我使用了必须在主线程上的领域,所以我做了一个处理程序post。这可能是问题所在吗?我还在调试,但我猜它可能在那里。
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();
}
感谢@Ci_ 我发现了这个问题。这显然是 Android Studio 1.4 Preview 的错误。或 Android SDK。
有关详细信息,请查看此处: