这是我使用的代码:
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();
}