2017-08-30 60 views
1

我建立它採用網頁視圖中xamarin的應用程序,我已經測試了他們的許多設備,如銀河S7邊緣。我想其中的原因不工作是因爲邊緣網屏的。但是,它在S7 Edge上正常工作,但在我嘗試過的S8 Edge設備上,它們會立即崩潰。Android應用程序崩潰的銀河S8僅邊沿

有隻有兩個,我使用類:

MainActivity:

namespace APPNAME.Android 
{ 
[Activity(Label = "APPNAME", Icon = "@drawable/icon", MainLauncher = 
false, 
    ConfigurationChanges = ConfigChanges.ScreenSize | 
    ConfigChanges.Orientation)] 

public class MainActivity : Activity 
{ 
    WebView web_view; 
    //private bool appeared; 
    public class HelloWebViewClient : WebViewClient 
    { 
     public override bool ShouldOverrideUrlLoading(WebView view, string 
     url) 
     { 
      view.LoadUrl(url); 
      return false; 
     } 
    } 

    protected override void OnCreate(Bundle bundle) 
    { 
     Window.RequestFeature(WindowFeatures.NoTitle); 
     base.OnCreate(bundle); 
     // Set our view from the "main" layout resource 
     SetContentView(Resource.Layout.Main); 

     web_view = FindViewById<WebView>(Resource.Id.webview); 
     web_view.Settings.JavaScriptEnabled = true; 
     web_view.SetWebViewClient(new HelloWebViewClient()); 

     web_view.LoadUrl("http://link.com"); 
    } 

    public override bool OnKeyDown(Keycode keyCode, KeyEvent e) 
    { 
     if (keyCode == Keycode.Back && web_view != null) 
     { 
      try 
      { 
       if (web_view.CanGoBack()) 
       { 
        Log.Debug("StackOverflow", "Allow browser back"); 
        // Toast.MakeText(this, "Going back", 
        ToastLength.Short).Show(); 
        web_view.GoBack(); 
        return true; 
       } 
      } 
      catch (Exception ex) 
      { 
       Log.Error("StackOverflow", ex.Message); 
      } 
     } 

     { 
      //Log.Error("StackOv20erflow", "Null webview..."); 
      //StartActivity(typeof(MainActivity)); 
      //Finish(); 
      OnBackPressed(); 
     } 
     //Log.Debug("StackOverflow", "Back button blocked"); 
     //Toast.MakeText(this, "Back button blocked", 
     ToastLength.Short).Show(); 
     return false; 
    } 

    public override void OnBackPressed() 
    { 
     Intent startMain = new Intent(Intent.ActionMain); 
     startMain.AddCategory(Intent.CategoryHome); 
     startMain.SetFlags(ActivityFlags.NewTask); 
     StartActivity(startMain); 

    } 

} 

} 

閃屏:

namespace APPNAME.Android 
{ 
[Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory = 
true)] 
public class SplashActivity : AppCompatActivity 
{ 
    static readonly string TAG = "X:" + typeof(SplashActivity).Name; 

    public override void OnCreate(Bundle savedInstanceState, 
    PersistableBundle persistentState) 
    { 
     base.OnCreate(savedInstanceState, persistentState); 
     Log.Debug(TAG, "SplashActivity.OnCreate"); 
    } 

    // Launches the startup task 
    protected override void OnResume() 
    { 
     base.OnResume(); 
     Task startupWork = new Task(() => { SimulateStartup(); }); 
     startupWork.Start(); 
    } 

    // Simulates background work that happens behind the splash screen 
    async void SimulateStartup() 
    { 
     //Log.Debug(TAG, "Performing some startup work that takes a bit of 
     time."); 
     //await Task.Delay(1000); // Simulate a bit of startup work. 
     //Log.Debug(TAG, "Startup work is finished - starting 
     MainActivity."); 
     //StartActivity(new Intent(Application.Context, 
     typeof(MainActivity))); 
     await Task.Delay(4000); 
     var intent = new Intent(this, typeof(MainActivity)); 
     intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.NewTask); 
     StartActivity(intent); 
    } 
    } 
} 

清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
android:versionName="1.0.0.8" package="APPNAME.android" 
android:installLocation="auto" android:versionCode="8"> 
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="25" /> 
<application android:label="APPNAME" android:icon="@drawable/Icon" 
android:debuggable="false"></application> 
<uses-permission android:name="android.permission.INTERNET" /> 
</manifest> 

不知道從哪裏開始修復發行因爲我不知道這是什麼原因。

任何幫助,將不勝感激!

+0

事件和處理程序請包括logcat的輸出也崩潰 –

+0

什麼在Logcat中? – SushiHangover

+0

您是否安裝了碰撞記者?如果沒有,請添加一個;-) – SushiHangover

回答

1

避免使用async void除非在事件處理程序中。而且也從來不創建一個new Task和使用task.Start

參考Async/Await - Best Practices in Asynchronous Programming

不過我勸你創建的SplashActivity

public class SplashActivity : AppCompatActivity { 

    static readonly string TAG = "X:" + typeof(SplashActivity).Name; 

    public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState) { 
     base.OnCreate(savedInstanceState, persistentState); 
     Log.Debug(TAG, "SplashActivity.OnCreate"); 
    } 

    // Launches the startup task 
    private event EventHandler Starting = delegate { } 
    protected override void OnResume() { 
     base.OnResume(); 
     //subscribe to event 
     Starting += Activity_Starting; 
     //raise event 
     Starting (this, EventArgs.Empty); 
    } 

    // Simulates background work that happens behind the splash screen 
    async void Activity_Starting(object sender, EventArgs e) { 
     //unsubscribe 
     Starting -= Activity_Starting; 
     //and carry on 
     //Log.Debug(TAG, "Performing some startup work that takes a bit of time."); 
     //await Task.Delay(1000); // Simulate a bit of startup work. 
     //Log.Debug(TAG, "Startup work is finished - starting MainActivity."); 
     //StartActivity(new Intent(Application.Context, typeof(MainActivity))); 
     await Task.Delay(4000); 
     var intent = new Intent(this, typeof(MainActivity)); 
     intent.AddFlags(ActivityFlags.ClearTop | ActivityFlags.NewTask); 
     StartActivity(intent); 
    } 
    } 
} 
+0

只是一個謹慎的詞來實施這個人..如果你+ =每個的onResume事件處理程序,那麼你會繼續乘以只需導航回到此活動即可運行此次數。 –

+0

@ Le-royStaines即使在事件處理程序中取消訂閱? (順便說一下)。通過這種立場,即使沒有像OP那樣的事件也會發生同樣的情況。思考? – Nkosi

+0

哎呦,我錯過了你取消訂閱處理程序。雖然這是最優雅的方式嗎?如果有人在其他地方舉辦活動而沒有訂閱呢? –