我正在为我的学校项目制作一个简单的浏览器,我正在尝试制作一个最喜欢的课程。这是代码:
public class Favorite extends Activity{
EditText etName;
EditText etAdress;
Button bAdd;
TextView tvDisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.favorite);
etName = (EditText) findViewById(R.id.etName);
etAdress = (EditText) findViewById(R.id.etAdress);
bAdd = (Button) findViewById(R.id.bAdd);
tvDisplay = (TextView) findViewById(R.id.tvDisplay);
bAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = etName.getText().toString();
String adress = etAdress.getText().toString();
if (adress != null) {
if (adress.contains("http://") || adress.contains("www.")
|| adress.contains(".com")
|| adress.contains(".ro")
|| adress.contains(".org")) {
try {
FileWriter WriteFile = new FileWriter(
"D:\\FACULTATE\\proiect\\Browser3\\favorites.txt");
BufferedWriter out = new BufferedWriter(WriteFile);
out.write(name + " " + adress);
tvDisplay.setText("Name: " + name
+ " Adress: " + adress);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
} else
tvDisplay
.setText("The adress you have entered is invalid");
} else {
tvDisplay.setText("You haven't entered anything");
}
}
});
}
}
它没有写入文件和TextView中......我做错了什么?在添加文件编写器和try / catch之前,它在textview中显示了两个字符串,但现在它没有。
答案 0 :(得分:0)
你的文件名错了 - 这看起来不像是一个android路径 - 更像是一些windows的东西:
D:\\FACULTATE\\proiect\\Browser3\\favorites.txt
答案 1 :(得分:0)
package com.example.testing;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
公共类收藏扩展活动
{
EditText etName;
EditText etAdress;
按钮b添加;
TextView tvDisplay;
@覆盖
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.favorite);
etName =(EditText)findViewById(R.id.etName);
etAdress =(EditText)findViewById(R.id.etAdress);
bAdd =(Button)findViewById(R.id.bAdd);
tvDisplay =(TextView)findViewById(R.id.tvDisplay);
bAdd.setOnClickListener(new View.OnClickListener()
{
@SuppressLint(" NewApi&#34)
@覆盖
public void onClick(查看v)
{
String name = etName.getText()。toString();
String adress = etAdress.getText()。toString();
if(地址!= null)
{
if(adress.contains(" http://")|| adress.contains(" www。")|| adress.contains(" .com&#34 ;)|| adress.contains(" .ro")
|| adress.contains("。组织&#34))
{
尝试
{
文件文件=新文件(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)," favorite.txt");
FileWriter WriteFile = new FileWriter(file);
BufferedWriter out = new BufferedWriter(WriteFile);
out.write(姓名+"" +地址);
tvDisplay.setText("名称:" +名称+"地址:" +地址);
out.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
其他
tvDisplay.setText("您输入的地址无效");
}
其他
{
tvDisplay.setText("你还没有输入任何内容");
}
}
});
}
}
不要'忘记添加
" uses-permission android:name =" android.permission.WRITE_EXTERNAL_STORAGE"
在AndroidManifest.xml中
它对我有用。