0

我需要在应用小部件中读取 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();
    }
}

}

应用小部件的主要活动名称为“小部件”。我需要在此活动“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();
}

对不起我的英语不好

4

1 回答 1

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 我希望它对您有所帮助:)

于 2012-09-03T08:48:36.910 回答