我需要在app Widget中读取txt文件。
我在名为“NASTAVENI”的外部集合活动中创建了txt:
@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();
}
}
}
app Widget的主要活动名称为“Widget”。我需要在此活动“Widget”中读取外部活动“NASTAVENI”的txt文件。 Activity Widget和NASTAVENI在同一个包中。
此代码错误(错误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();
}
抱歉我的英文
答案 0 :(得分:0)
我不确定你是否只想要一个从txt读取的小部件,或者你也想要一个“活动”。 为了读取txt文件,我用来创建一个读取文件的类,并在这样的函数中回复我需要的东西:
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();
}
}
}
对于小部件,您需要创建一个具有以下内容的小部件的类:
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";
}
我不确定我的代码是否完美,可能你需要完成它,因为我没有粘贴整个函数。但如果您有疑问,这是一个很好的教程:vogella.com 我希望它可以帮助你:)