我从资源目录中读取简单的txt时遇到问题。我无法弄清楚它为什么不起作用以及出了什么问题。
以下是我写的方法代码:
private string ReadFile(){
var stream = Assets.Open ("sampleText.txt");
StreamReader sr = new StreamReader (stream);
string text = sr.ReadToEnd ();
sr.Close ();
return text;
}
这是错误:
Java.Lang.NullPointerException: Exception of type 'Java.Lang.NullPointerException' was thrown.
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in /Users/builder/data/lanes/2970/46c3f7e0/source/mono/external/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:143
at Android.Runtime.JNIEnv.CallNonvirtualObjectMethod (IntPtr jobject, IntPtr jclass, IntPtr jmethod) [0x00084] in /Users/builder/data/lanes/2970/46c3f7e0/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:596
at Android.Content.ContextWrapper.get_Assets () [0x0005f] in /Users/builder/data/lanes/2970/46c3f7e0/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Content.ContextWrapper.cs:154
我在Windows上运行我的整个应用程序没有问题,只是我在android上运行它有问题。试过各种各样的事情,比如AssetManager的例子,但也没有用。
文本文件标记为AndroidAsset。
这是我的TextChangeBench
:
public class TextChangeBench : Activity
{
public TextChangeBench (){}
private void SaveFile(string[] tab){
string saveLocation = "sampleTextReworked.txt";
StreamWriter sw = new StreamWriter (saveLocation);
foreach (string s in tab) {
sw.Write (s);
}
sw.Close ();
}
private string ReadFile(){
var stream = Assets.Open ("sampleText.txt");
StreamReader sr = new StreamReader (stream);
string text = sr.ReadToEnd ();
sr.Close ();
return text;
}
public void ChangeText(){
try{
File.Delete("sampleTextReworked.txt");
}catch(FileNotFoundException e){Console.WriteLine (e);}
try{
string text = ReadFile ();
char c;
string[] newTab = new string[text.Length];
for (int i = 0; i < text.Length; i++)
{
c = (char)text [i];
if (Char.IsUpper(c))
{
newTab[i] = text[i].ToString().ToLower();
}
else if (Char.IsLower(c))
{
newTab[i] = text[i].ToString().ToUpper();
}
else
{
newTab[i] = text[i].ToString();
}
}
SaveFile(newTab);
}
catch(Exception e){Console.WriteLine ("{0} ", e);}
}
}
这是我的MainActivity
:
[Activity (Label = "csBench", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity
{
protected override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
SetContentView (Resource.Layout.Main);
Button startButton = FindViewById<Button> (Resource.Id.start);
startButton.Click += delegate {
MathBench mb = new MathBench ();
TextChangeBench tcb = new TextChangeBench ();
PassedTime pt = new PassedTime ();
EditText et = (EditText)FindViewById (Resource.Id.textInfo);
for (int i = 0; i < 5; i++) {
pt.StartMeasuring ();
//mb.Silnia (25);
//mb.Fibonacci (32);
//mb.BubbleSort ();
tcb.ChangeText ();
if (i == 4) {
pt.StopMeasuring ();
et.SetText(pt.ReturnResult(), TextView.BufferType.Normal);
//et.SetText(pt.ReturnResult().ToString());
//Console.WriteLine(pt.ReturnResult ());
pt.ResetTimers ();
}
}
};
}
}
欢迎任何帮助,提前谢谢。
答案 0 :(得分:7)
发生此错误是因为手动创建活动实例(例如TextChangeBench
)会绕过Android框架设置过程,使活动处于无效状态。
活动用作用户交互点,将视图/屏幕绑定到应用程序中的逻辑。由于TextChangeBench
仅实现应用程序逻辑,因此不需要从Activity
派生,并且更适合作为普通的C#类。
删除继承为activity以简化TextChangeBench
的代码。这意味着我们无法再访问Assets
属性,因此无法检索资产。我们可以通过使用全局应用程序上下文来解决这个问题:
var stream = Android.App.Application.Context.Assets.Open("sampleText.txt");
删除活动继承和使用全局上下文访问资产管理器的组合将修复Java.Lang.NullPointerException
。
最终代码如下所示:
public class TextChangeBench
{
private void SaveFile(string[] tab)
{
string saveLocation = "sampleTextReworked.txt";
StreamWriter sw = new StreamWriter(saveLocation);
foreach (string s in tab)
{
sw.Write(s);
}
sw.Close();
}
private string ReadFile()
{
var stream = Android.App.Application.Context.Assets.Open("sampleText.txt");
StreamReader sr = new StreamReader(stream);
string text = sr.ReadToEnd();
sr.Close();
return text;
}
public void ChangeText()
{
try
{
File.Delete("sampleTextReworked.txt");
}
catch (FileNotFoundException e) { Console.WriteLine(e); }
try
{
string text = ReadFile();
char c;
string[] newTab = new string[text.Length];
for (int i = 0; i < text.Length; i++)
{
c = (char)text[i];
if (Char.IsUpper(c))
{
newTab[i] = text[i].ToString().ToLower();
}
else if (Char.IsLower(c))
{
newTab[i] = text[i].ToString().ToUpper();
}
else
{
newTab[i] = text[i].ToString();
}
}
SaveFile(newTab);
}
catch (Exception e) { Console.WriteLine("{0} ", e); }
}
}
请参阅: