Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在android中从另一个线程更新ui_Java_Android_Multithreading - Fatal编程技术网

Java 在android中从另一个线程更新ui

Java 在android中从另一个线程更新ui,java,android,multithreading,Java,Android,Multithreading,我想改变android的用户界面 我的主类生成第二个类,然后第二个类调用主类的方法。主类中的方法应该更新UI,但程序在运行时崩溃 我该怎么办 我的主要班级: public class FileObserverActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceStat

我想改变android的用户界面

我的主类生成第二个类,然后第二个类调用主类的方法。主类中的方法应该更新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;
        }
    }
}
尝试如下

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

不能从主线程以外的线程调用UI方法。你应该使用这个方法


@它会崩溃,因为你不能用另一个线程更改UI。如果你想更改UI中的某些内容,你应该使用它自己的线程来表示UI线程。什么是上下文?9行结束?thansk…它工作了。with ContextWrapper context=new ContextWrapper(fileobserveractivity);
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;
        }
    }
}