我遇到了多線程問題。警告是:「類型MenuThread的方法start()未定義」。我該怎麼辦Android中的多線程警告
public void run() {
if (whichMethodToCall == 1) {
}
else if (whichMethodToCall == 2) {
}
}
我遇到了多線程問題。警告是:「類型MenuThread的方法start()未定義」。我該怎麼辦Android中的多線程警告
public void run() {
if (whichMethodToCall == 1) {
}
else if (whichMethodToCall == 2) {
}
}
MenuThread正在實現Runnable
接口。這不是一個線程。如果它在不同的線程運行通過MyThread的實例給Thread對象
Thread thread = new Thread(new MenuThread(i));
thread.start();
使用new Thread(thread).start()
。
問題的代碼:
MenuThread thread = new MenuThread(i);
上面一行創建MenuThread
它實現Runnable
接口。但它不是一個線程,因此
thread.start();
是非法的。
權的方式從Runnable
實例創建線程
MenuThread thread = new MenuThread(i);
(new Thread(thread)).start();
您可以通過兩種不同的方式創建線程。看看有關的oracle文檔thread creation
創建Thread實例的應用程序必須提供將在該線程中運行的代碼。有兩種方法可以做到這一點:
Provide a Runnable object.
的Runnable
接口定義了一個方法,運行,意味着包含線程執行的代碼。 The Runnable object is passed to the Thread constructor
public class HelloRunnable implements Runnable {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
(new Thread(new HelloRunnable())).start();
}
}
Subclass Thread
。 Thread類本身實現Runnable
,儘管它的run方法什麼也不做。應用程序可以繼承Thread
,提供它自己的實現運行
public class HelloThread extends Thread {
public void run() {
System.out.println("Hello from a thread!");
}
public static void main(String args[]) {
(new HelloThread()).start();
}
}
定義的run方法? –
@blahfunk run方法是在MenuThread類中定義的,但其他類包含上面的代碼,並沒有看到啓動方法。我應該怎麼做 – xiac
發佈定義您的MenuThread類的代碼,請 –