我创建了一个android程序,我们(用户)可以在Edit Text中设置id等,然后该值将被保存到sdcard中的.txt文件中。我已经可以这样做了(在sdcard中创建一个.txt文件。现在我正在尝试在我的应用程序中的其他活动中读取.txt文件,并在此活动开始/开始时在编辑文本中插入该值..我需要那个所以我可以在我的应用程序中设置一个例如“id”,然后每次用户制作一个表单条目数据,之前设置的一些数据例如“id”已经插入到条目活动中的编辑文本那里..当我我试图从.txt(我之前在其他Activity中构建的文件)读取值,并希望在Activity启动时将该值插入EditText,我收到类似这样的错误 “活动无法bla ... bla .. bla ..”“java.lang.null.pointerexception” 请帮助我在我的代码中想念我,谢谢
当我尝试阅读.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);
}
}
}
答案 0 :(得分:0)
您可以使用此行从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);