问题
I need read txt file in app Widget.
I create txt in external set activity with name "NASTAVENI":
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.set);
editor=(EditText)findViewById(R.id.editor);
Button btn=(Button)findViewById(R.id.close);
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
phoneNo = editor.getText().toString();
finish();
}
});
}
public void onResume() {
super.onResume();
try {
InputStream in=openFileInput("notes.txt");
if (in!=null) {
InputStreamReader tmp=new InputStreamReader(in);
BufferedReader reader=new BufferedReader(tmp);
String str;
StringBuilder buf=new StringBuilder();
while ((str = reader.readLine()) != null) {
buf.append(str+"\n");
}
in.close();
editor.setText(buf.toString());
}
}
catch (java.io.FileNotFoundException e) {
}
catch (Throwable t) {
Toast
.makeText(this, "Exception: "+t.toString(), 2000)
.show();
}
}
/*BACK**************************************************************************************************************************************/
public void onPause() {
super.onPause();
try {
OutputStreamWriter out=
new OutputStreamWriter(openFileOutput("notes.txt", 0));
out.write(editor.getText().toString());
out.close();
}
catch (Throwable t) {
Toast
.makeText(this, "Exception: "+t.toString(), 2000)
.show();
}
}
}
Main activity for app Widget is with name "Widget". I need read txt file of external activity "NASTAVENI" in this activity "Widget". Activity Widget and NASTAVENI are in the same package.
This code is bad (error java.lang.NullPointerException)
try {
InputStream in=openFileInput("notes.txt");
InputStreamReader tmp=new InputStreamReader(in);
BufferedReader reader=new BufferedReader(tmp);
String str;
StringBuilder buf=new StringBuilder();
while ((str = reader.readLine()) != null) {
buf.append(str+"\n");
}
in.close();
editor.setText(buf.toString());
phoneNo = "77";
}
catch (java.io.FileNotFoundException e) {
Toast toast=Toast.makeText(context, "Error FFE!", 1000);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
catch (Throwable t) {
Toast toast=Toast.makeText(context, "Error T!" +t.toString(), 1000);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
Sorry for my English
回答1:
I am not sure if you want just a widget that reads from a txt or you want also "an activity". For the reading the txt file, I use to create a class that reads the file and give me back what I need, in a function like this:
ShowLine(){
try {
InputStream is = mContext.getAssets().open(monthFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
// Skips lines
Log.w("day", String.valueOf(monthFile));
for (int i = 0; i< DayMonth-1; i++) {
reader.readLine();
}
Line = reader.readLine(); // read the line of the day
Name= Line.substring(Line.indexOf("*") + 1,Line.indexOf("-"));// I read between + and (my data is like: DAY * NAME -)
dia= ""+DayMonth+" de "+ monthNames[Month]+"" ;
Log.w("Name", String.valueOf(Name)); //to check that it works
} catch (IOException e) {
e.printStackTrace();
}
}
}
For the widget , you need to create a class that is a Widget with :
public class Widget extends AppWidgetProvider {
String dia = "1 Feb.";
String Name= "Pavel";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
mContext = context;
Resources res = mContext.getResources();
ComponentName thisWidget = new ComponentName(context,Widget.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
for(int widgetIds: allWidgetIds){
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
//Log.w("Widget test", String.valueOf(number));
try{
ShowLine();
Log.w("Line 1", String.valueOf(Name));
}catch (Exception e) {
Name= "error";
}
I am not sure my code works perfect, probably you have to complete it a bit, because I didn't paste the whole functions. But If you have doubts , this is a good tutorial: vogella.com I hope it helps you :)
来源:https://stackoverflow.com/questions/10381719/read-txt-file-in-app-widget