Java 从SD卡插入到EditText中读取.txt时出错
我制作了一个android程序,我们(用户)可以通过编辑文本设置id等,然后该值将保存到SD卡的.txt文件中。我已经可以这样做了(在SD卡中创建一个.txt文件。现在我正在尝试在我的应用程序的其他活动中读取该.txt文件,并在该活动开始/开始时将该值插入编辑文本中。)我需要它,以便我可以设置例如“id”在我的应用程序中,只有一次,然后每次用户创建表单条目数据时,之前设置的一些数据(例如“id”)已经插入到条目活动中的编辑文本中。当我尝试从.txt(我以前在其他活动中构建的文件)中读取值时要在活动开始时将该值插入EditText,我遇到了一个类似这样的错误 “活动无法bla..bla..bla..”“java.lang.null.pointerexception” 请帮助我在代码中遗漏了什么,谢谢 这是我在尝试读取.txt文件时在活动中的代码:Java 从SD卡插入到EditText中读取.txt时出错,java,android,android-activity,nullpointerexception,android-sdcard,Java,Android,Android Activity,Nullpointerexception,Android Sdcard,我制作了一个android程序,我们(用户)可以通过编辑文本设置id等,然后该值将保存到SD卡的.txt文件中。我已经可以这样做了(在SD卡中创建一个.txt文件。现在我正在尝试在我的应用程序的其他活动中读取该.txt文件,并在该活动开始/开始时将该值插入编辑文本中。)我需要它,以便我可以设置例如“id”在我的应用程序中,只有一次,然后每次用户创建表单条目数据时,之前设置的一些数据(例如“id”)已经插入到条目活动中的编辑文本中。当我尝试从.txt(我以前在其他活动中构建的文件)中读取值时要在活
package com.asrimobile;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
public class Email extends Activity implements OnClickListener{
//inisilisasi elemen-elemen pada layout
private Button Send;
private EditText Subject;
private EditText Message;
private TextView tv_attach;
private ImageButton Attachment;
private EditText edTo_email;
String file_path;
private static final int PICK_IMAGE = 100;
int columnindex;
//Uri URI = null;
//inisialisasi kontroller/Data Source
private DBDataSource dataSource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.email);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"myconfig2.txt");
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line=br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
}
catch (IOException e) {
e.printStackTrace();
}
edTo_email.setText(text);
edTo_email = (EditText) findViewById(R.id.to_email);
Send = (Button) findViewById(R.id.button_send);
Attachment = (ImageButton) findViewById(R.id.button_attachment);
Subject = (EditText) findViewById(R.id.subject);
Message = (EditText) findViewById(R.id.message);
tv_attach = (TextView) findViewById(R.id.tv_attach_id);
Attachment.setOnClickListener(this);
Send.setOnClickListener(this);
// instanstiasi kelas DBDataSource
dataSource = new DBDataSource(this);
//membuat sambungan baru ke database
dataSource.open();
}
String to_email = edTo_email.getText().toString();
//KETIKA Tombol Submit Diklik
@Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.button_attachment:
openGallery();
break;
case R.id.button_send:
//String surat3 = edTo_email.getText().toString();
String surat1 = Subject.getText().toString();
String surat2 = Message.getText().toString();
//File pngDir = new File(Environment.getExternalStorageDirectory(),"");
//if (!pngDir.exists())
//{
// pngDir.mkdirs();
//}
//File pngfile=new File(pngDir, "asri.xls");
//Uri pngUri =Uri.fromFile(pngfile);
//kirim email
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
//need this prompt email clients only
emailIntent.setType("plain/text");
//emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {surat3});
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, surat1);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, surat2);
//emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, pngUri);
//emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
//emailIntent.setType("message/rfc822");
ArrayList<String> fileList = new ArrayList<String>();
fileList.add(Environment.getExternalStorageDirectory()+"/asri.xls");
fileList.add(Environment.getExternalStorageDirectory()+"/kk.png");
ArrayList<Uri> uris = new ArrayList<Uri>();
for (int i=0;i<fileList.size();i++)
{
File fileIn = new File(fileList.get(i));
Uri u = Uri.fromFile(fileIn);
uris.add(u);
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
//String[] filePaths = new String[] {"sdcard/entri_pengajuan.csv"};
//for (String file : filePaths)
//{
// File fileIn = new File(file);
// Uri u = Uri.fromFile(fileIn);
// uris.add(u);
//}
//emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
//if (URI != null) {
// emailIntent.putExtra(Intent.EXTRA_STREAM, URI);
//}
startActivity(Intent.createChooser(emailIntent, "Send email using..."));
break;
}
}
private void openGallery() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
PICK_IMAGE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_IMAGE && resultCode == RESULT_OK) {
/**
* Get Path
*/
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
columnindex = cursor.getColumnIndex(filePathColumn[0]);
file_path = cursor.getString(columnindex);
tv_attach.setText(file_path);
//URI = Uri.parse("file://" + file_path);
cursor.close();
Bitmap yourSelectedImage = BitmapFactory.decodeFile(file_path);
File sdcard = Environment.getExternalStorageDirectory();
File f = new File (sdcard, "kk.png");
FileOutputStream out = null;
try {
out = new FileOutputStream(f);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
yourSelectedImage.compress(Bitmap.CompressFormat.PNG, 90, out);
}
}
}
package com.asrimobile;
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.ArrayList;
导入android.net.Uri;
导入android.os.Bundle;
导入android.os.Environment;
导入android.provider.MediaStore;
导入android.app.Activity;
导入android.content.Intent;
导入android.database.Cursor;
导入android.graphics.Bitmap;
导入android.graphics.BitmapFactory;
导入android.view.view;
导入android.view.Window;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ImageButton;
导入android.widget.TextView;
公共类电子邮件扩展活动实现OnClickListener{
//inisilisasi elemen elemen pada布局图
私人按钮发送;
私人编辑文本主体;
私人短信;
私人文本视图电视连接;
私人图像按钮附件;
私人编辑文本edTo_电子邮件;
字符串文件路径;
私有静态最终整数选取图像=100;
int列索引;
//Uri=null;
//inisialisasi kontroller/数据源
私有数据库数据源;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE\u自定义\u标题);
setContentView(R.layout.email);
getWindow().setFeatureInt(Window.FEATURE\u CUSTOM\u TITLE,R.layout.Window\u TITLE);
文件sdcard=Environment.getExternalStorageDirectory();
File File=新文件(SD卡,“myconfig2.txt”);
StringBuilder text=新的StringBuilder();
试一试{
BufferedReader br=新的BufferedReader(新文件读取器(文件));
弦线;
而((line=br.readLine())!=null){
text.append(行);
text.append('\n');
}
br.close();
}
捕获(IOE异常){
e、 printStackTrace();
}
edTo_email.setText(文本);
edTo_电子邮件=(EditText)findViewById(R.id.to_电子邮件);
发送=(按钮)findviewbyd(R.id.Button\u发送);
附件=(ImageButton)findViewById(R.id.button_附件);
主题=(编辑文本)findViewById(R.id.Subject);
Message=(EditText)findViewById(R.id.Message);
tv_attach=(TextView)findviewbyd(R.id.tv_attach_id);
附件.setOnClickListener(本文件);
Send.setOnClickListener(this);
//instanstiasi kelas数据库源
dataSource=新的DBDataSource(此);
//membuat sambungan baru ke数据库
dataSource.open();
}
字符串to_email=edTo_email.getText().toString();
//KETIKA Tombol提交Diklik
@凌驾
公共void onClick(视图v){
开关(v.getId())
{
外壳R.id.按钮\附件:
openGallery();
打破
案例R.id.按钮\u发送:
//字符串surat3=edTo_email.getText().toString();
字符串surat1=Subject.getText().toString();
字符串surat2=Message.getText().toString();
//File pngDir=新文件(Environment.getExternalStorageDirectory(),“”);
//如果(!pngDir.exists())
//{
//pngDir.mkdirs();
//}
//File pngfile=新文件(pngDir,“asri.xls”);
//Uri pngUri=Uri.fromFile(pngfile);
//kirim电子邮件
Intent emailIntent=新的Intent(android.content.Intent.ACTION\u SEND\u MULTIPLE);
//仅需要此提示电子邮件客户端
emailIntent.setType(“纯/文本”);
//emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,新字符串[]{surat3});
emailIntent.putExtra(android.content.Intent.EXTRA_主题,surat1);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,surat2);
//emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,pngUri);
//emailIntent.putExtra(Intent.EXTRA_流,URI);
//emailIntent.setType(“message/rfc822”);
ArrayList fileList=新建ArrayList();
添加(Environment.getExternalStorageDirectory()+“/asri.xls”);
add(Environment.getExternalStorageDirectory()+“/kk.png”);
ArrayList URI=新的ArrayList();
对于(int i=0;i,您可以使用此行从SD卡读取文件并将其设置为edittext
File logFile = new File("sdcard/text.txt");
FileInputStream fIn = new FileInputStream(logFile);
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = "";
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow + "\n";}
edittext.settext(aBuffer);