在下面的代碼中,Eclipse生成警告「這個Handler類應該是靜態的或者可能發生泄漏」。這個Handler類應該是靜態的或者可能發生泄漏:final Handler
public class MyActivity extends Activity implements Runnable
{
final Handler handler = new Handler()
{
@Override
public void handleMessage(Message message)
{
String sResult = (String) message.obj;
if((sResult != null) && (sResult != ""))
{
MyNonStatic = (TableLayout) findViewById(R.id.tableLayout); // any non-static method
}
return;
}
};
public void run()
{
final Message message = handler.obtainMessage(1, MyFunction(context));
handler.sendMessage(message);
}
public String MyFunction(Context context)
{
return "MyNewString";
}
}
我在網站上回顧了很多主題,但沒有得到解決方案。請幫我看看這段代碼?
添加:我需要在handleMessage()中調用非靜態方法(例如findViewById())!
這是幾乎相同的,因爲這問題:HTTP://計算器。 com/questions/11407943/this-handler-class-should-be-static-or-leaks-might-occurrence-incominghandler – ThomasW