C# Xamarin意图空指针

C# Xamarin意图空指针,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,你好,我正在尝试在Xamarin上创建一个html文件。制作完文件后,我试图打开它,但我一直得到一个空指针Java.Lang.NullPointerException 是不是因为意图不同?我试图在invoicePage.xaml.cs中实现这个意图,但每当我调用StartActivityContent时,我总是会收到格式错误 我的代码如下: invoicePage.xaml.cs using Android.App; using Android.Content; using System; us

你好,我正在尝试在Xamarin上创建一个html文件。制作完文件后,我试图打开它,但我一直得到一个空指针Java.Lang.NullPointerException

是不是因为意图不同?我试图在invoicePage.xaml.cs中实现这个意图,但每当我调用StartActivityContent时,我总是会收到格式错误

我的代码如下:

invoicePage.xaml.cs

using Android.App;
using Android.Content;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace AIFieldService.Views
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class InvoicePage : ContentPage
    {
        public InvoicePage()
        {
            InitializeComponent();

            var htmlSource = new HtmlWebViewSource();
            htmlSource.Html =
              @"<html>
                <body>
                    <h1>Xamarin.Forms</h1>
                    <p>Welcome to WebView.</p>
                </body>
            </html>";
            web.Source = htmlSource;
        }

        public async void OnCancelClicked(Object sender, EventArgs e)
        {
            await Navigation.PopAsync();
        }

        public void OnPrintClicked(Object sender, EventArgs e)
        {
           htmlMaker hm = new htmlMaker(web.Source.ToString());
           hm.write();
        }
    }
}

您需要将表单代码与Android代码分开,是的,您面临的问题的一个方面可能是因为Android操作系统没有正确创建htmlMaker活动。千万不要使用new MyActivity来实例化活动类,因为操作系统不会调用OnCreate等方法

我建议使用or从表单共享代码调用Android项目代码,这样您就可以运行特定于Android的代码来编写文件并打开浏览器。我将使用消息中心,因为它更简单。因此,从OnPrintClicked处理程序开始:

public void OnPrintClicked(Object sender, EventArgs e)
{
   MessagingCenter.Send<InvoicePage, string>(this, "html", web.Source.ToString());
}

那么,您的解决方案是如何设置的?看起来你在混合平台特定的Android代码和Xamarin.Forms代码?基本上是Xamarin.Forms应用程序还是使用Xamarin.Forms页面的Android应用程序?Xamarin.Forms应用程序,当我在网上查看时,这是大多数人用Xamarin打开应用程序浏览器所做的。也许用Xamarin Android,用Xamarin.Forms,需要做不同的事情。因此,这就解释了为什么您将平台无关的Xam.Forms代码与特定于平台的Xamarin.Android代码混合在一起。在意图部分,您如何公开应用程序的外部。当我运行代码时,我通过Intent.getData将“[file path]暴露在app之外。我在AndroidManfest.xml中读到了一些内容,但不知道如果路径是Android.OS.Environment.GetExternalStoragePublicDirectoryAndroid.OS.Environment.DirectoryDocuments.AbsolutePath;,会是什么样子;。在应用程序之外公开是什么意思?提供商允许其他应用从您的应用获取数据。您打算如何告知浏览器应用程序使用您的内容提供商?以下是关于在应用程序中设置ContentProvider的文档:我还验证了上述代码可以在Chrome浏览器中打开html文件。它可以创建html文件,但当我运行应用程序时,它会在到达this.startactivity帐篷时,通过Intent.getData向我提供超出应用程序的[file path];我不明白,你用的是上面的代码吗?
public void OnPrintClicked(Object sender, EventArgs e)
{
   MessagingCenter.Send<InvoicePage, string>(this, "html", web.Source.ToString());
}
Xamarin.Forms.MessagingCenter.Subscribe<InvoicePage, string>(this, "html", (sender, html) => 
    {
        //I changed this path to be a public path so external apps can access the file. 
        //Otherwise you would have to grant Chrome access to your private app files
        var documentsPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDocuments).AbsolutePath;
        Directory.CreateDirectory(documentsPath);

        //This creates the full file path to file
        string filePath = System.IO.Path.Combine(documentsPath, "invoice.html");

        //writes to file (no need to create it first as the below will create if necessary)
        File.WriteAllText(filePath, html);

        //opens file
        Android.Net.Uri uri = Android.Net.Uri.FromFile(new Java.IO.File(filePath));

        Intent intent = new Intent(Intent.ActionView, uri);
        intent.AddFlags(ActivityFlags.NewTask);
        intent.SetClassName("com.android.chrome", "com.google.android.apps.chrome.Main");

        this.StartActivity(intent);
    });