2011-08-10 9 views
1

アンドロイドでUIを変更したい。アンドロイドの別のスレッドからuiを更新する

メインクラスは2番目のクラスを作成し、2番目のクラスはメインクラスのメソッドを呼び出すと、ランタイムにUIは更新されますが、プログラムクラッシュが発生します。

どうすればよいですか?

私のメインクラス:

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) 
    { 
     mySTR = absolutePath+path+"\t"+event; 
      tv.setText(mySTR); // program crash here! 
    } 
} 

と私の第二のクラス:

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; 
     } 
    } 
} 
+0

クラッシュのログはありますか? – ania

+1

@aniaそれはあなたが別のスレッドでUIを変更することができないのでクラッシュします。UIで何かを変更したい場合は、独自のスレッド平均UIスレッドを使用する必要があります。 –

答えて

8

を次のように糸。 Activity#runOnUiThread()メソッドを使用する必要があります。

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; 
     } 
    } 
} 
2

てみてくださいあなたは、メイン以外のスレッドからUIのメソッドを呼び出すことはできません

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; 
     } 
    } 
} 
+0

whats context? 9行終了? –

+0

thansk ...それはworks.with ContextWrapperコンテキスト=新しいContextWrapper(fileobserveractivity); –

関連する問題