gpt4 book ai didi

安卓 ZygoteInit$MethodAndArgsCaller.run() "source not found"

转载 作者:行者123 更新时间:2023-11-29 01:55:52 25 4
gpt4 key购买 nike

我构建了一个示例应用程序,它从用户那里获取输入并保存在我的应用程序 Assets 文件夹中的文本文件“sample”中。

这是我的 MainActivity.java,

 package com.example.sampledatabaseapp;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

protected static final android.content.Context Context = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

EditText txtname=(EditText)findViewById(R.id.editText1);
String name=txtname.getText().toString();

AssetManager am=Context.getAssets();
try {

InputStream is=am.open("sample");
OutputStreamWriter out=new OutputStreamWriter(openFileOutput("sample",MODE_APPEND));
out.write(name);

out.close();
Toast.makeText(Context,"Text Saved !",Toast.LENGTH_LONG).show();

} catch (IOException e) {

e.printStackTrace();
Toast.makeText(Context,"Sorry Text could't be added",Toast.LENGTH_LONG).show();
}


}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

}

我的 activity_main.xml,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:ems="10"
android:hint="@string/Enter_Name">

<requestFocus />
</EditText>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_alignRight="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="82dp"
android:text="@string/Save" />

</RelativeLayout>

我开始逐行调试,当调试器调试“AssetManager am=Context.getAssets();”行时在我的 MainActivity.java 文件中,它显示“未找到 ZygoteInit$MethodAndArgsCaller.run() 的源”。我下载了源代码并附上。

可是,我还是不知道哪里做错了。 这是我的堆栈跟踪,

  12-27 13:32:16.467: D/InputEventConsistencyVerifier(1803): KeyEvent: ACTION_UP but key was not down.
12-27 13:32:16.467: D/InputEventConsistencyVerifier(1803): in android.widget.Button{40d02568 VFED..C. .F....I. 135,168-345,216 #7f070001 app:id/button1}
12-27 13:32:16.467: D/InputEventConsistencyVerifier(1803): 0: sent at 5236119000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=5236119, downTime=5235983, deviceId=0, source=0x101 }

12-27 13:32:16.908: W/Trace(1803): Unexpected value from nativeGetEnabledTags: 0
12-27 13:32:16.938: W/Trace(1803): Unexpected value from nativeGetEnabledTags: 0
12-27 13:32:16.969: W/Trace(1803): Unexpected value from nativeGetEnabledTags: 0
12-27 13:32:16.969: W/Trace(1803): Unexpected value from nativeGetEnabledTags: 0
12-27 13:32:17.008: W/Trace(1803): Unexpected value from nativeGetEnabledTags: 0
12-27 13:32:17.038: W/Trace(1803): Unexpected value from nativeGetEnabledTags: 0
12-27 13:32:17.118: W/Trace(1803): Unexpected value from nativeGetEnabledTags: 0
12-27 13:32:17.148: W/Trace(1803): Unexpected value from nativeGetEnabledTags: 0

我不知道我是否使用了正确的方法在 android 中将文本写入文本文件。如有不妥请指教。

请任何人帮助!!

谢谢。

最佳答案

MODE_APPEND当然是将数据追加到一个已经存在的文件中。如果你想创建一个新的,你应该使用 MODE_PRIVATE

OutputStreamWriter out=new OutputStreamWriter(openFileOutput("sample",MODE_PRIVATE));

当你阅读文件时,

        try {
FileInputStream fileInputStream;
fileInputStream = openFileInput("sample");
byte[] readBytes = new byte[fileInputStream.available()];
fileInputStream.read(readBytes);
String readString = new String(readBytes);
Log.v("readString", readString);
fileInputStream.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}

好的,删除这一行怎么样?

AssetManager am=Context.getAssets();

您不必使用 AssetManager。

然后你使用写这个。

try {
FileOutputStream fileOutputStream = openFileOutput("myfile.txt", MODE_PRIVATE);
String writeString = "test";
fileOutputStream.write(writeString.getBytes());
} catch (FileNotFoundException e) {
} catch (IOException e) {
}

关于安卓 ZygoteInit$MethodAndArgsCaller.run() "source not found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15215725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com