2017-03-15 12 views
0

私はxamarinでAndroidプロジェクトを作成しようとしています。SplashActivityの後にMainActivityを起動すると、Androidアプリが終了する

私はAndroidを学ぼうとしていますが、私はちょっと立ち往生しています。私はMainActivityを持っていますが、これはアプリ起動時に起動されたアクティビティです。今私は別のアクティビティを意味するスプラッシュ画面を追加していますが、私は明らかにこれが開始されたアクティビティであることを望み、一定期間後にメインアクティビティを起動させます。最初にスプラッシュを開始しました。良い。スプラッシュアクティビティからメインアクティビティを起動しようとすると、終了します。私はxamarinマニュアルの例を使って、このスプラッシュアクティビティをAppCompActivityではなくActivityを継承させるような小さな調整をしています。

https://developer.xamarin.com/guides/android/user_interface/creating_a_splash_screen/

注意、MainActivityは、いくつかの時間のため非常によく働いている、それだけでクラッシュしたり、それがメインのランチャーアプリの活動ではないことを今閉じています。

私はただの重要なエントリが欠落していることを知っています。これはおそらくマニフェストファイルや宣言がCsファイルにないことがわかります。これはアプリが通常は静かに閉じている。多くのマニフェスト宣言は、実行時にcsファイルで行われます。なぜなら、それらは私が見て使ったサンプルからのものであるからです。ここに私のコードです。誰か私が逃していることを教えてもらえますか?長さは申し訳ありませんが、私は何を含むか含まないか分かりません。

これはスプラッシュアクティビティです。

[Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory = true)] 
    public class SplashActivity : Activity 
    { 
     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(2000); // Simulate a bit of startup work. 
      Log.Debug(TAG, "Startup work is finished - starting MainActivity."); 
      StartActivity(new Intent(Application.Context, typeof(MainActivity))); 
     } 
    } 

ここは私のMainActivityです。マニフェストファイルに表示される権限に反映されているように、ここに投稿されたものよりも多くのものがありますが、ここに投稿するには時間がかかりすぎました。また、このアクティビティは、別のアクティビティ。私は、要求に応じて、よりを追加することができますが、私はこの問題は

using System; 
using System.IO; 
using System.Net; 
using System.Text; 
using Android.Webkit; 
using Android.App; 
using Android.Content; 
using Android.Widget; 
using Android.OS; 
using Java.Interop; 
using Android.Util; 
using MyNameSpace.Services; 
using Android.Preferences; 
using Android.Graphics.Drawables; 

namespace MyNameSpace 
{ 
    [Activity(Label = "My Agent", Theme = "@android:style/Theme.NoTitleBar", ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize | Android.Content.PM.ConfigChanges.KeyboardHidden)] 

    public class MainActivity : Activity 
    { 
     WebView webview = null; 
     urlPrefix = "http://example.com/"; 

     protected override void OnCreate(Bundle bundle) 
     { 

      base.OnCreate(bundle); 
      webview = new WebView(this); 
      webview.Settings.JavaScriptEnabled = true; 
      webview.Settings.SetGeolocationEnabled(true); 
      webview.SetWebViewClient(new MyWebViewClient(this)); 
      webview.SetWebChromeClient(new MyWebChromeClient(this)); 
      SetContentView(webview); 
      webview.LoadUrl(urlPrefix + "connection.aspx"); 

      //Set Notification bar but make the activity intent pending on user click of notification msg 

      var intent = new Intent(this, typeof(MainActivity)); 
      intent.AddFlags(ActivityFlags.SingleTop); 
      var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.UpdateCurrent); 

      Notification.Builder builder = new Notification.Builder(this) 
        .SetContentTitle("My Agent Running") 
        .SetContentText("Show in forground") 
        .SetSmallIcon(Resource.Drawable.hippo72) 
        .SetContentIntent(pendingIntent); 

      // Build the notification: 

      notification = builder.Build(); 

      // Get the notification manager: 

      notificationManager = GetSystemService(Context.NotificationService) as NotificationManager; 

      // Publish the notification: 

      notificationManager.Notify(notificationId, notification); 

    public class MyWebViewClient : WebViewClient 
    { 
     private readonly Context _context; 
     public MyWebViewClient(Context context) 
     { 
      _context = context; 
     } 
     public bool shouldOverrideUrlLoading(WebView view, String url) 
     { 
      var uri = Android.Net.Uri.Parse(url); 
      var intent = new Intent(Intent.ActionView, uri); 
      _context.StartActivity(intent); 
      return true; 
     } 
    } 

    public class MyWebChromeClient : WebChromeClient 
    { 
     private readonly Context _context; 

     public MyWebChromeClient(Context context) 
     { 
      _context = context; 
     } 

     public override void OnGeolocationPermissionsShowPrompt(string origin, GeolocationPermissions.ICallback callback) 
     { 
     callback.Invoke(origin, true, false); 
    } 
} 

}

私のマニフェストファイル

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mydomain.myagent" android:versionCode="1" android:versionName="1.0"> 
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="23" /> 
<application android:label="My Agent" android:icon="@drawable/hippo72"></application> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.INSTALL_PACKAGES" /> 
<uses-permission android:name="android.permission.INSTALL_LOCATION_PROVIDER" /> 
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.LOCATION_HARDWARE" /> 
<uses-permission android:name="android.permission.PERSISTENT_ACTIVITY" /> 
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 
<uses-permission android:name="android.permission.BLUETOOTH" /> 
<uses-permission android:name="android.permission.CALL_PHONE" /> 
<uses-permission android:name="android.permission.SEND_SMS" /> 

答えて

0

はSplashActivityにこのコードを試してみてくださいどこか別の場所だと思います。

完了();タスクなしと非同期/待機なし

[Activity(MainLauncher = true, Theme = "@style/MyTheme.Splash", NoHistory = true)] 
public class SplashActivity : Activity 
{ 
    protected override void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 
     Intent startup = new Intent(this, typeof(MainActivity)); 
     StartActivity(startup); 
     Finish(); 
    } 
} 
+0

私は初心者ですが、スプラッシュページが実際に何かをする前にMainActivityを開始するように見えます。誰かがそれを見るかどうかわかりません。 – Bobh

+0

この記事に助言をすることはできますかhttp://www.puresourcecode.com/dotnet/post/XamarinForms-(Android)-Workaround-For-Splash-Screen-With-Logo? – Enrico

+0

@Bobhは、あなたのアプリケーションが完全に初期化されたときにMainActivityを開始します。このチュートリアルをお読みくださいhttps://www.bignerdranch.com/blog/splash-screens-the-right-way/ – Mohamed

関連する問題