6

Mam wbudowany Cordova WebvView jako komponent do mojego projektu, wszystko działa dobrze z wyjątkiem jednej rzeczy; po naciśnięciu przycisku Wstecz pojawia się błąd w LogCat, który mówi "Odbiornik niezarejestrowany!" Nie sądzę, abym zarejestrował odbiornik. Istnieje również przykładowy projekt na GitHub here. Ten sam błąd występuje również po uruchomieniu tej aplikacji.Osadzanie Cordova WebView, odbiornik nie jest zarejestrowany

Co chcę zrobić, to osadzić Cordova WebView w moim projekcie Android i uruchomić niektóre funkcje javascript.

Oto moja główna działalność;

public class MainNativeViewController extends FragmentActivity implements CordovaInterface, 
    JavaScriptListener { 

FragmentTransaction transaction; 
RelativeLayout childBrowser; 
RelativeLayout dialogBox; 
RelativeLayout emailComposer; 

private ExecutorService threadPool; 

CordovaWebViewFragment cordovaWebViewFragment; 

public CordovaWebView cordovaWebView; 

public LayoutInflater inflater; 
CordovaPlugin mActivityResultCallback; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.fragment_container); 

    overridePendingTransition(R.anim.fade_in, R.anim.fade_out); 

    threadPool = Executors.newCachedThreadPool(); 

    cordovaWebView = SingleTonCordovaWebView.getCordovaWebView(this); 
    cordovaWebView.loadUrl("file:///android_asset/www/invoke_native_view.html"); 

    FragmentManager manager = getSupportFragmentManager(); 

    ListFragment listFragment = new ListFragment(); 

    manager.beginTransaction().add(R.id.fragment_container, listFragment).commit(); 
} 

@Override 
public void showFragment(String fragmentName) { 

    /* 
    * This method is used to create and replace a fragment to the 
    * container. according to name passed through here. 
    */ 

    transaction = getSupportFragmentManager().beginTransaction(); 
    transaction.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left, 
      R.anim.slide_in_left, R.anim.slide_out_right); 

    if (fragmentName.equals("error")) { 
     DialogCreator dialog = new DialogCreator(this); 
     dialog.createDialog("Error !", "Fragment Name is wrong,", "Check fragment name", false); 
    } else { 
     // fragment name is ignored for different cases, it will be used for 
     // further proporties. 
     // Just checking out the error in the case its not right parameter. 

     NativeViewTestFragment testFragment = new NativeViewTestFragment(fragmentName); 

     transaction.replace(R.id.fragment_container, testFragment); 
     transaction.addToBackStack(null); 
     transaction.commit(); 
    } 
} 

@Override 
protected void onNewIntent(final Intent intent) { 
    super.onNewIntent(intent); 

    // Forward to plugins 
    if ((this.cordovaWebView != null) && (this.cordovaWebView.pluginManager != null)) { 
     this.cordovaWebView.pluginManager.onNewIntent(intent); 
    } 
} 

@Override 
public void cancelLoadUrl() { 

} 

@Override 
public ExecutorService getThreadPool() { 
    return threadPool; 
} 

@Override 
public Activity getActivity() { 
    return this; 
} 

@Override 
public Context getContext() { 
    return this; 
} 

@Override 
public Object onMessage(final String id, final Object data) { 
    // return getCordovaFragment().onMessage(id, data); 
    return null; 
} 

@Override 
public void setActivityResultCallback(CordovaPlugin arg0) { 
    // TODO Auto-generated method stub 

} 

@Override 
public void startActivityForResult(CordovaPlugin plugin, Intent intent, int requestCode) { 
    mActivityResultCallback = plugin; 
    startActivityForResult(intent, requestCode); 
} 

@Override 
/** 
* Called when the system is about to start resuming a previous activity. 
*/ 
protected void onPause() { 
    super.onPause(); 

    // Send pause event to JavaScript 
    this.cordovaWebView.loadUrl("javascript:try{cordova.fireDocumentEvent('pause');}catch(e){console.log('exception firing pause event from native');};"); 

    // Forward to plugins 
    if (this.cordovaWebView.pluginManager != null) { 
     this.cordovaWebView.pluginManager.onPause(true); 
    } 
    threadPool.shutdown(); 
    threadPool = null; 
} 

@Override 
/** 
* Called when the activity will start interacting with the user. 
*/ 
protected void onResume() { 
    super.onResume(); 

    threadPool = Executors.newCachedThreadPool(); 

    if (this.cordovaWebView == null) { 
     return; 
    } 

    // Send resume event to JavaScript 
    this.cordovaWebView 
      .loadUrl("javascript:try{cordova.fireDocumentEvent('resume');}catch(e){console.log('exception firing resume event from native');};"); 

    // Forward to plugins 
    if (this.cordovaWebView.pluginManager != null) { 
     this.cordovaWebView.pluginManager.onResume(true); 
    }  
} 

@Override 
/** 
* The final call you receive before your activity is destroyed. 
*/ 
public void onDestroy() { 
    super.onDestroy(); 
    if (cordovaWebView.pluginManager != null) { 
     cordovaWebView.pluginManager.onDestroy(); 
    } 

    if (this.cordovaWebView != null) { 

     // Send destroy event to JavaScript 
     this.cordovaWebView 
       .loadUrl("javascript:try{cordova.require('cordova/channel').onDestroy.fire();}catch(e){console.log('exception firing destroy event from native');};"); 

     // Load blank page so that JavaScript onunload is called 
     this.cordovaWebView.loadUrl("about:blank"); 

     // Forward to plugins 
     if (this.cordovaWebView.pluginManager != null) { 
      this.cordovaWebView.pluginManager.onDestroy(); 
     } 
    } else { 
     // this.endActivity(); 
    } 
} 
} 

EDIT: Oto wynik LogCat;

11-23 12:25:36.117: E/AndroidRuntime(9645): FATAL EXCEPTION: main 

11-23 12:25:36.117: E/AndroidRuntime(9645): java.lang.RuntimeException: Unable to  destroy activity  {org.apache.cordova.example/okan.apps.nativeview.MainNativeViewController}:  java.lang.IllegalArgumentException: Receiver not registered:  [email protected] 
11-23 12:25:36.117: E/AndroidRuntime(9645):  at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3655) 
11-23 12:25:36.117: E/AndroidRuntime(9645):  at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3673) 
11-23 12:25:36.117: E/AndroidRuntime(9645):  at android.app.ActivityThread.access$2900(ActivityThread.java:125) 
11-23 12:25:36.117: E/AndroidRuntime(9645):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
11-23 12:25:36.117: E/AndroidRuntime(9645):  at android.os.Handler.dispatchMessage(Handler.java:99) 
11-23 12:25:36.117: E/AndroidRuntime(9645):  at android.os.Looper.loop(Looper.java:123) 
11-23 12:25:36.117: E/AndroidRuntime(9645):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
11-23 12:25:36.117: E/AndroidRuntime(9645):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-23 12:25:36.117: E/AndroidRuntime(9645):  at java.lang.reflect.Method.invoke(Method.java:521) 
11-23 12:25:36.117: E/AndroidRuntime(9645):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
11-23 12:25:36.117: E/AndroidRuntime(9645):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
11-23 12:25:36.117: E/AndroidRuntime(9645):  at dalvik.system.NativeStart.main(Native Method) 
11-23 12:25:36.117: E/AndroidRuntime(9645): Caused by: java.lang.IllegalArgumentException: Receiver not registered: [email protected] 
11-23 12:25:36.117: E/AndroidRuntime(9645):  at android.app.ActivityThread$PackageInfo.forgetReceiverDispatcher(ActivityThread.java:793) 
11-23 12:25:36.117: E/AndroidRuntime(9645):  at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:814) 
11-23 12:25:36.117: E/AndroidRuntime(9645):  at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:331) 
11-23 12:25:36.117: E/AndroidRuntime(9645):  at org.apache.cordova.Device.onDestroy(Device.java:98) 
11-23 12:25:36.117: E/AndroidRuntime(9645):  at org.apache.cordova.api.PluginManager.onDestroy(PluginManager.java:317) 
11-23 12:25:36.117: E/AndroidRuntime(9645):  at okan.apps.nativeview.MainNativeViewController.onDestroy(MainNativeViewController.java:204) 
11-23 12:25:36.117: E/AndroidRuntime(9645):  at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3642) 
11-23 12:25:36.117: E/AndroidRuntime(9645):  ... 11 more 

EDIT:

skończyło się roztworem. Używałem Cordova Web View w Fragmentie, usunąłem go z fragmentu i umieściłem w tym samym xml z fragmentem kontenera. Teraz działa bez błędów. To samo w przykładowym projekcie w GitHub, CordovaWebView jest w FrameLayout.

Odpowiedz

1

Nie trzeba wdrożyć całość zniszczyć płynąć samodzielnie, można użyć następujących:

public void onDestroy() { 
    LOG.d("Destroying the View", "onDestroy()"); 
    super.onDestroy(); 

    if (this.cwv != null) { 
    this.cwv.handleDestroy(); 
    } 
} 

nadzieję, że to pomaga.

+0

Hej, próbowałem swoje rozwiązanie, ale nadal ten sam błąd. mówi: 'Aktywność MainNativeViewController wyciekła IntentReceiver [email protected], która została oryginalnie zarejestrowana tutaj. Czy brakuje połączenia z wyrejestrowaniemReceiver()? ' – osayilgan

+0

Hej, chyba znalazłem sposób, nazwałem' this.cwv.removeAllViews(); ' przed handleDestroy(), Wygląda na to, że działa. Nie dostaję już błędu, ale nie znam jego właściwej drogi? – osayilgan

+0

** EDYCJA: ** W ten sposób zostanie zniszczony bez błędu, ale w przypadku, gdy spróbuję ponownie uruchomić aplikację po jej zniszczeniu, pojawia się kolejny błąd, który mówi: "java.lang.IllegalStateException: Nie można tego wykonać akcja po onSaveInstanceState "' – osayilgan

4

Rozwiązałem to w moim fragmentu poprzez dodanie następujących do onDestroyView()

@Override 
    public void onDestroyView() { 
     super.onDestroyView(); 
     mCordovaWebView.handleDestroy(); 

    } 
+0

Nie jestem pewien, której wersji Cordova używasz, ale (Prawdopodobnie rozwiązano to w nowszych wersjach), tym razem, gdy zadałem to pytanie, Twoje rozwiązanie nie zadziałało. – osayilgan

+0

Używam 3.6.4 na Androida. Jednak moje rozwiązanie powyżej wydaje się mieć wprowadzone '' 'java.lang.IllegalArgumentException: Receiver not registered: org.apache.cordova.App''' na wyjściu ... Skończyło się na tym, ponieważ za pomocą przycisku Wstecz do Ukończenie aplikacji dało mi "Działalność" MainNativeViewController wyciekł IntentReceiver [email protected], który został pierwotnie zarejestrowany tutaj. Brakuje połączenia z wyrejestrowaniemReceiver() '' ' Co wydaje się być przeciwieństwem tego, co pierwotnie widziałeś. – theSociableme

+0

Jeśli widzisz ten wyjątek, to jest to ten sam problem po dodaniu powyższego rozwiązania @Yoel. Więc zamiast używać onDestroy, jeśli wywołasz handleDestroy w onDestroyView rozwiązuje problem? – osayilgan

Powiązane problemy