i want to change UI in android.
my main class make a second class then second class call a method of main class.method in main class should update UI but program cra
Try as follows
public class FileObserverActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView1);
tv.setText("new world");
MyFileObserver myFileObserver = new MyFileObserver("/sdcard/", this);
myFileObserver.startWatching();
registerReceiver(onBroadcast,new IntentFilter("abcd"));
}
String mySTR = "";
TextView tv ;
public void event(String absolutePath,String path,int event)
{
mySTR = absolutePath+path+"\t"+event;
tv.setText(mySTR); // program crash here!
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(onBroadcast);
}
private final BroadcastReceiver onBroadcast = new BroadcastReceiver() {
@Override
public void onReceive(Context ctxt, Intent i) {
// do stuff to the UI
event(absolutePath, path, event);
}
};
}
public class MyFileObserver extends FileObserver
{
public String absolutePath;
FileObserverActivity fileobserveractivity;
public MyFileObserver(String path,FileObserverActivity foa)
{
super(path, FileObserver.ALL_EVENTS);
absolutePath = path;
fileobserveractivity = foa;
}
@Override
public void onEvent(int event, String path)
{
if (path == null)
{
return;
}
else if(event!=0)
{
context.sendBroadcast(new Intent("abcd"));
//fileobserveractivity.event(absolutePath, path, event);
}
else
{
return;
}
}
}
You can't call UI methods from threads other than the main thread. You should use Activity#runOnUiThread() method.
public class FileObserverActivity extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView1);
tv.setText("new world");
MyFileObserver myFileObserver = new MyFileObserver("/sdcard/", this);
myFileObserver.startWatching();
}
String mySTR = "";
TextView tv ;
public void event(String absolutePath,String path,int event)
{
runOnUiThread(action);
}
private Runnable action = new Runnable() {
@Override
public void run() {
mySTR = absolutePath+path+"\t"+event;
tv.setText(mySTR);
}
};
}
public class MyFileObserver extends FileObserver
{
public String absolutePath;
FileObserverActivity fileobserveractivity;
public MyFileObserver(String path,FileObserverActivity foa)
{
super(path, FileObserver.ALL_EVENTS);
absolutePath = path;
fileobserveractivity = foa;
}
@Override
public void onEvent(int event, String path)
{
if (path == null)
{
return;
}
else if(event!=0)
{
fileobserveractivity.event(absolutePath, path, event);
}
else
{
return;
}
}
}