开发者

I am trying to update a textview from a different activity and it keeps crashing

On a button click I am reading some information from a file and I need to display this in a textview, this code works while I am in the activity with the layout but if I am in a different screen the same code doesn't work.

String sdcard = Environment.getExternalStorageDirectory().getAbsolutePath();
File file = new File(sdcard + "/Mult/","boardname.cfg");
//Read text from file
try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;
   while ((line = br.readLine()) != null) {
       Global.defaultboard = line;
   }
   TextView defaultboard = (TextView) findViewById(R.id.currentboard);
   defaultboard.setText(Global.defaultboard);
}    
catch 开发者_开发技巧(IOException e) {
    //You'll need to add proper error handling here
}
 finish();

Can anyone see whats wrong? if I remove the lines aor comment the lines of the textview it doesn't crash.

Thanks

MrC

OK

My Solution is - my value is stored in a global variable so I use the onResume() like this to refresh the textview.

public void onResume()
{
         super.onResume();
         TextView defaultboard = (TextView) findViewById(R.id.currentboard);
         defaultboard.setText(Global.defaultboard);
 }

MrC


you can only update UI views when running in the main thread. If you do stuff in a different thread use the runOnUIThread method of the activity passing a little Runnable that does the actual work.


Well, each activities has his own layout so you can't update items through different activities directly.

What you need is to take a look at "Starting Activities and Getting Results"


Have you ever thought of using SharedPreferences?

  1. Make a string for the text you want to display in this class.

  2. Write this string to shared preferences using putString("key", value).

  3. In the activity that has the TextView you need to update, initialize it to something as to not get a NullPointerException. Then load the string from your SharedPreferences and set the text from there.

  4. If you want to be able to set it dynamically, you can make a updateText() method in the activity that has the TextView to change it, because if the activity is still running, it will not update as onCreate() is not called. Just be sure to check what state it's in, because if that activity is not currently in existence (onDestroy() has been called), you will get another exception.

Hope this helps. I have found that SharedPreferences are the answer to a lot of my problems. If you need help, just let me know and I'll try to help you out.


OK

My Solution is - my value is stored in a global variable so I use the onresume like this to refresh the textview

public void onResume()
{
         super.onResume();
         TextView defaultboard = (TextView) findViewById(R.id.currentboard);
         defaultboard.setText(Global.defaultboard);
 }

Thanks to everyone for answering, I like some of these ideas and am looking into how they work!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜