我正在使用Xamarin Visual Studio for Android应用程序,尽管构建成功, 应用程序抛出异常,在我的异常的屏幕截图下方
尽管我正在寻找解决方案,但找不到相关的解决方案。
SplashActivity:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using Android.Support.V7.App;
using Android.Views;
using Android.Widget;
namespace MySchool.Droid
{
[Activity(Label = "MySchool", Icon = "@mipmap/icon", Theme = "@style/Theme.splash", MainLauncher = true, NoHistory = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class SplashActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
protected override void OnResume()
{
base.OnResume();
Task startup = new Task(StartUp);
startup.Start();
}
async void StartUp()
{
await Task.Delay(100);
StartActivity(new Intent(Application.Context, typeof(MainActivity)));
}
}
}
MainActivity:
using System;
using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
namespace MySchool.Droid
{
[Activity(Label = "myschools", Theme = "@style/MainTheme", ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
}
}
Splash_Screen.xml
<?xml version="1.0" encoding="utf-8" ?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/colorAccent" />
<padding android:left="0dip"
android:top="0dip"
android:right="0dip"
android:bottom="0dip" />
</shape>
</item>
<item>
<drawable android:src="@drawable/myschool"
android:gravity="center" >
</drawable>
</item>
</layer-list>
实际上,我试图使用我的splash_screen图标使我的SplashActivity成为主启动器,以便在我的应用程序上线时显示该代码,以下是代码片段,以及顶部的异常屏幕截图。
预先感谢
答案 0 :(得分:2)
首先,我看不到OnCreate()
中设置了您的Splash Activity布局:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Splash_Screen); //This Line is needed
}
这是Splash_Screen.axml
在文件夹Resources\layout\Splash_Screen.axml
中的样子
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:foo="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/splash_background"
android:scaleType="fitXY" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:paddingBottom="20dp">
<TextView
android:id="@+id/lblBottomText"
android:text="Hello and welcome to stackoverflow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/black"
android:textSize="20dp"
android:gravity="center" />
</RelativeLayout>
</FrameLayout>
在您的SplashActivity.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using Android.Support.V7.App;
using Android.Views;
using Android.Widget;
namespace MySchool.Droid
{
[Activity(MainLauncher = true, Label = "MySchool", Icon = "@mipmap/icon")]
public class SplashActivity : AppCompatActivity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Splash);
new Handler().PostDelayed(() =>
{
StartActivity(new Intent(this, typeof(MainActivity)));
Finish();
}, 2500);
}
}
}
然后确保在Splash.axml
文件夹内Resources
文件夹layout
内添加以下文件MySchool.Droid\Resources\layout\Splash.axml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/lblBottomText"
android:text="Hello and welcome to stackoverflow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" />
</FrameLayout>
您应该能够看到“启动画面”,然后持续2.5秒钟。